Implement atosl on linux and atosl on linux

Source: Internet
Author: User

Implement atosl on linux and atosl on linux

Refer to the following link for more technical tips: Alibaba Cloud blog

Preface

On linux, how does one implement an atos tool (iOS symbolic parsing) similar to that on mac )?

Analyze problems

On github I found the Open Source implementation several years ago, [https://github.com/dechaoqiu/atosl] (https://github.com/dechaoqiu/atosl)

The compiled atosl tool usually works normally. parsing errors only occur in special circumstances, mainly in the following ways:

1. using apps written in Swift, the compiled atosl will surely be parsed incorrectly (garbled form)

2. Using some functions implemented by C ++, the strings parsed by the compiled atosl are garbled.

3. parsing errors (garbled form) may occur when parsing the framwork framework of iOS systems)

4. Some crash information of the user cannot locate the specific line no. The compiled atosl service can only parse the offset.

IOS dSYM file structure analysis

During iOS App development, we will use Xcode to package and generate. the xarchive package file can be managed and exported through Xcode's Organizer tool. iOS developers are familiar with these processes and will not repeat them here, the dSYM file generated after packaging.

DSYM is a directory. After opening it, we will find a binary file such

It can be seen that iOS uses the DWARF file structure (Debug With Attributed RecordFormats) as a standard for debugging file structure, and the structure is quite complex.

[Https://www.prevandres.net/dwarf.html] (https://www.prevanders.net/dwarf.html)

An important role of the dSYM file is that when our program crashes, we can see the call stack information through the crash log or other methods. Through the log information, we do not know where the specific file is located, and the information provided by the binary file is very useful at this time, through this tool, we can use the tool to symbolize it. on Mac OS X platform, Xcode comes with tools such as atos, so that you can directly locate the specific location of a file. The usage is as follows:

There is a dwarfdump tool in Mac OS to parse the DWARF file. Obviously, the parsed information cannot meet all our requirements.

Dwarfdump-a SwfitTest

For more information about the internal structure, see iOS System Analysis (2) Mach-O binary file parsing.

Ideas

Download the source code of atosl from github, which is written in C.

Example of adding Cxx and Swift errors

The results of make test are as follows. It is obvious that there is a problem between Cxx and Swift symbolic. This is the problem I want to solve.

Return to mac OS x and use the xcrun atos processing provided by xcode for correct parsing, (So what I need to do is to implement the same atos-\> input and output on a linux platform similar to that on mac OS x)

I used a MachOView tool when studying and analyzing the Mach-O file. Then I used the tool to open the dSYM file QuartzCore, I found that the strings parsed in SymTables are garbled, but the magic thing happened. When I move my mouse over a line of garbled code, I return the correct parsing string, this indicates that MachOView can correctly parse the C ++ name. Decisive github searches for the source code. From here, we know that the compiler will perform some operations on the function during the compilation process. The following analyzes the behavior of the compiler.

Mangled symbol names (rename)

C/Cxx

In a language such as C, any given name (Symbol) can only correspond to a unique function or data, and name mangling is not required ). Even so, if you look at a typical pure C binary symbol table, you will find that each function name has a prefix (underline), as shown below:

This simple "mangling" has a long history, but it is of little use, but it still has some effect on compatibility and consistency. By convention, the names defined in C are underlined, but the global symbols defined in a pure assembly are not (although many assembly language authors consider the definition of underscores in advance for consistency ).

Objective-C

Its symbolic names do not disagree or conflict with each other. The Objective-C method is implemented in the form of-[class selector] (#), in addition, objective-c does not allow the same class to use different parameters to overload the same selectors.

Cxx

A simple name without additional information may disagree, so some processing must be done as follows:

Because a function corresponds to two functions with different parameters, which is a legal definition in cxx, we cannot simply generate two \ _ function symbols, because the linker will not know how to link them, different function implementations cannot be distinguished. Therefore, the cxx compiler uses a strict set of Encoding Rules "mangles" (reorganizing) symbols.

Swift

1. The first character '-' is required for the Swift symbol.

2. '-t' indicates the global symbol of Swift.

Solution

It is also feasible to restore the symbols by yourself according to this rule, but it is still time-consuming and there may be bugs.

Fortunately, Xcode comes with a tool that allows you to view the original features of these mangled names without having to implement them again.

Solution Process

Since apple provides a tool, I don't need to write it myself. I can call it directly. The tool address in the xcode directory is as follows:

The first solution is to call swift-demangle directly in the pipeline communication atosl.

Solution 2: link the swift-demangle.dylib to the program.

Correct parsing. Next, we should try to port this applet to the Linux platform.

I guess this is a tool provided by Swift. Google found that Swift is open-source at the source code level and supports Linux.

Compile Swift on linux

When compiling the configuration environment (Ubuntu 14.04), there is a hole in the compilation process (the memory must be 5 GB or above, and the hard disk must be 30 GB or above)

Warning: If you encounter an error similar to clang: error: unable to execute command: Killed, do not think about it. It is the memory burst. You may have succeeded after trying it several times.

If everything works properly, the compilation will be completed in one hour (my hardware environment MacBookAir 1.4 GHz CPU 8 GB memory SSD, which took nearly one hour ).

Solution 1: After swift compilation is complete, ELF executable file exists in build/xxx/bin.

Solution 2: Use the compiled library file

Not found in lib directory. so dynamic library, wondering for a long time (the swift compilation script uses Cmake) Darwin refers to the core of the mac system kernel (including the xnu kernel and Unix Shell environments ), comment it out and you can (Linux shared library ). to compile the static library, you need to modify the cmake script, for example:

After the shared library is compiled, it is directly linked to atosl. swift can only compile it on Ubutun. If your atosl will run on other Linux releases, it is best to use static links for all dependent libraries.

2

Make test

The test found that the offset of the cxx and Swift test samples (the line no found cannot be parsed), no one has maintained the code on github for a long time, and finally it was resolutely rewritten.

End

(If you have any questions, please contact email: liutianshxkernel@gmail.com)

Link: http://blog.tingyun.com/web/article/detail/1342

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.