Deep understanding of the Android NDK log symbology

Source: Internet
Author: User

absrtact: Today's app basically collects the log information when the crash is reported, whether using a third-party cloud platform or building a cloud service, we will upload the so dynamic library with debug information, realize the cloud log symbolization and cloud visual management, In this paper, the composition structure of so dynamic library is analyzed in depth.

CSDN Mobile will continue to be the essence of mobile development for you to explore the technical topics of mobile development, including mobile applications, development tools, mobile games and engines, smart hardware, the Internet of things and other aspects. If you would like to contribute, participate in the translation of content, or seek a near-carpenter report, please send an email to tangxy#csdn.net (please change # to @).

Objective

For code and product protection, almost all non-open source apps will be confusing code. This way, when the crash information is collected, it needs to be symbolized to restore the code information so that the developer can locate the bug. Depending on the use of the SDK and the NDK, Android crashes fall into two categories: Java crashes and C + + crashes. Java crashes are symbolized by the Mapping.txt file, which is relatively straightforward and intuitive. The symbolic use of C + + crashes requires using some of the NDK tools that Google comes with, such as Ndk-stack, Addr2line, Objdump, and so on. This article does not discuss how to use these tools, interested friends can refer to the previous Yin Chunpeng wrote another article, "How to locate the Android NDK encountered in the development of errors", which is described in detail.

The NDK-based Android development generates a dynamic-link library (so), which is generated based on C/s + + compilation. The dynamic-link library is widely used under Linux, and the Android system is Linux-based, so the compilation of the NDK so library follows the same rules, except that the Google NDK encapsulates the relevant cross-compiler tools.

Ndk-build compile-time generates two so libraries of the same name, located in different directories/project path/libs/armeabi/xxx.so and/project path/obj/local/armeabi/ Xxx.so, comparing two so files will find a large difference in volume. The former will be published with the app, so as small as possible, while the latter contains a lot of debugging information, mainly for GDB debugging, of course, the NDK's log symbolic information is also included therein.

In-depth analysis of so dynamic library composition structure

This paper is mainly for the so dynamic library containing debugging information, in-depth analysis of its composition structure. Before you begin, talk about the purpose or benefits of doing so. Now the app basically collects the log information when the crash is escalated, whether using a third-party cloud platform or setting up a cloud service yourself, upload the so dynamic Library with debug information to enable the cloud log to symbolize and cloud-based visualization management.

The fast iteration of mobile apps makes it necessary to store and manage every version of the debug so library, which contains a lot of information that is not symbolic. If we only mention the information required for symbolization, then the volume of the symbolized file will be reduced in order of magnitude. You can also add custom information such as the version number of the app in a custom symbolic file to automate deployment of symbolic extraction, uploading to the cloud, Cloud analytics, and visualization. In addition, from a technical point of view, the developer will no longer be afraid to see the "unresolved symbol" linking errors, you can more calmly debugging the C + + crash or do some hacking operations.

Start by readelf to see what's different about the so library in two different directories.

As you can see clearly, there are 8 more of the so libraries that contain debug information. Debug_ entries that begin with the. Symtab and. Strtab entries. The essence of symbolization is to restore the code's original statement and the corresponding line number through the address information in the stack, so it is only necessary to parse the. Debug_ lines and. Symtab, and finally get the following information can be symbolized.

C85    c8b     willcrash       jni/hello-jni.c:27-29c8b    c8d     willcrash       jni/hello-jni.c:32c8d    c8f     jni_onload      jni/hello-jni.c:34c8f    c93     jni_onload      jni/hello-jni.c:35c93    c9d     JNI _onload      jni/hello-jni.c:37

Typically, the target file is divided into three categories: The relocatable file, the executable file, and the shared object file, which are called Elf (executable and linking format), and so the dynamic library belongs to the third class shared object, its overall organizational structure is as follows:

ELF Header

The ELF header file header is structured as follows, recording the offset and size information of the other files in the file. Here is an example of 32bit:

typedef struct {unsigned char e_ident[ei_nident];          Elf32_half E_type;   Target file types, such as relocatable, executable, and shared object Elf32_half e_machine;   Specify the specific architecture you want, such as the Intel 80386,motorola 68000 Elf32_word e_version;       Target file version, Ei_version elf32_addr e_entry in e_ident;   Specifies the entry point address, such as C executable file entry is _start (), instead of main () Elf32_off E_phoff;   The offset of the program Header table Elf32_off E_shoff;  The offset of the section Header table is Elf32_word e_flags;  Processor-related flags elf32_half e_ehsize; Represents the size of the ELF header section elf32_half e_phentsize;   The size of each item in the Program Header table Elf32_half E_phnum;  Program Header table contains how many items elf32_half e_shentsize;  The size of each item in section Header table Elf32_half E_shnum;  Section Header table contains how many items elf32_half e_shstrndx; The index of a subkey in the section Header table that contains the string name of all sections} ELF32_EHDR; 

Where E_ident is a fixed 16 byte size array, called Elf identification, contains the processor type, file encoding format, machine type, etc., the structure is as follows:

Sections

This section contains all the information except the ELF header, Program Header table, and the Header table. The section Header table allows you to find basic information about each of these sections, such as name, type, offset, and so on.

Let's take a look at the contents of section header, still taking 32-bit as an example:

typedef struct {elf32_wordsh_name;  Specifies the name of the section, which is the index of the string table in the Elf32_wordsh_type; Specifies the classification of section elf32_wordsh_flags; The bit of the field represents the different section attribute elf32_addrsh_addr; If a section appears in a memory image, the field represents the address of the first byte of the Elf32_offsh_offset; Specifies the offset of section in the file elf32_wordsh_size; Specifies the size of bytes occupied by section Elf32_wordsh_link; The indexelf32_wordsh_info of the associated section Header table; Additional information, meaning depends on the type of section elf32_wordsh_addralign; Specifies the address to which the constraint elf32_wordsh_entsize;   If the section contains a table, the value specifies the size of each subkey in the table} ELF32_SHDR;

The section header's sh_name can be used to find the specified section, such as. debug_line,. Symbol,. Strtab.

String Table

The String table contains a sequence of characters ending in 0, with the last byte set to 0, indicating the end of all character sequences, such as:

The String table also belongs to the section, except that its offset is specified directly in the E_shstrndx field in the ELF header. The String table is read by starting at the specified index until a rest is encountered. For example, to get the name of the section in the header of the sh_name, assuming sh_name = 7, start with the 7th index of the string table byte stream (note here starting from 0) and read to the first rest (index=18), The name read to IS. Debug_line.

Symbol Table

This section contains information about the symbolic definition of a program, such as function definitions, variable definitions, and so on, each of which is defined as follows:

# Symbol Table entrytypedef struct {elf32_wordst_name;   The index of the symbol string table elf32_addrst_value;  Symbol-related values, depending on the type of symbol elf32_wordst_size;   The size of the symbol content unsigned charst_info;   The type of symbol and its attributes unsigned charst_other;  The visibility of symbol, such as the public property of the class Elf32_halfst_shndx;  The index of the section header associated with this symbol} Elf32_sym;

The types of symbol include the following:

Where Stt_func is the function symbol we're looking for. The corresponding function name (such as Jni_onload) is then obtained through st_name from the symbol string table. When the symbol type is Stt_func, St_value represents the start address of the symbol, and (st_value+st_size) represents the end address of the symbol.

Review the two sections of the. Symtab and. Strtab mentioned earlier, corresponding to the symbol section and symbol String.

DWARF (Debugging with attributed Record Formats)

Dwarf is a debug file format, and many compilers and debuggers use it for source debugging (GDB, etc.). Although it is a standalone target file format, it is often embedded in elf files. The 8. debug_* sections that were previously seen through Readelf are all in dwarf format. This article will discuss only the symbolic-related. Debug_line section, for more dwarf information, see the contents of the reference.

The. Debug_line section contains the line number information, which can be used to debug the source code by corresponding to the machine instruction address. Debug_line is made up of a number of children, each of which contains a description of the size of a similar data, called the statement program Prologue. Prologue provides information for decoding program directives and jumps to other statements, which contain fields that exist in binary format order:

The machine instructions used here can be divided into three categories:

Here does not do machine instruction explanation, interested in, you can view the contents of the reference.

Through. Debug_line, we can finally get the following information: file path, filename, line number, and start address.

Finally, let's summarize the entire symbolic extraction process:

    1. From the ELF header to learn 32bit or 64bit, as well as the big or small end, based on this read the following content;
    2. Gets the position of the section Header table in the file from the ELF header;
    3. Read the section Header Table for the location of the. Debug_line,. Symtab, and. Strtab three sections in the text;
    4. Read. Symtab and. Strtab two sections, and finally get the name, start address, and end address of all function symbol;
    5. Read. Debug_line to obtain the file name, path, line number, and start address according to dwarf format;
    6. Compare the results obtained in steps 4 and 5 and combine them to form the final result.
Reference documents
    • How to locate errors encountered in Android NDK development
    • How debuggers Work:part 3-debugging information
    • ELF (executable and linking Format)
    • The DWARF debugging Standard

Author Profile:

Jia Zhikai Testin Technical director, mainly responsible for the crash Analysis project Android platform Architecture design, performance and stability optimization.

  All say the programmer's high wages, but very little understanding of their overtime pain, you are not every time in the mind, according to the time to reduce the wages are less, so will want to cry in the heart, or raise wages, or raise wages, or raise wages, why?? Because don't let us work overtime, this is impossible!!!

Want to subvert your work model? Want to reduce your overtime? Join us and explore the free mode of our programmers!

A native app for programmers to share knowledge and skills as a reward for online interactive interactive platform.

We have a top technical team of nearly 20 people, as well as excellent product and operations teams. Team leaders have more than 10 years of experience in the industry.

Now that we are recruiting the original heroes, you will be working with us to change the way programmers work and change the world of programmers! There will also be generous rewards. As our original participant, you will experience this programmer artifact with us, you can offer professional advice, we will adopt it humbly. Everyone will be a hero, and you will be the hero we need! You can also invite your friends to participate in this heroic recruiting interaction.

We will not delay you too much time, we only need your professional opinion, as long as you take 1 hours from one months, you can save two hours a day, everything is for our own!

To? Or not?

Connector Person code: 1955246408 (QQ)

Deep understanding of the Android NDK log symbology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.