Find gdb dynamic library method

Transfer: IT Superman

When GDB can not display information so dynamic library or when the information is incorrect display, usually due to library search path errors, use set sysroot, set solib-absolute-prefix, set solib-search-path to specify library search path.

1. set sysroot and set solib-absolute-prefix is ​​the same command, in fact, set sysroot is set solib-absolute-prefix alias.

2. set solib-search-path set dynamic library search path, the search command may be provided a plurality of paths, using the path between ":" separated (in Linux as colon, DOS, and semicolon in Win32).

3. set solib-absolute-prefix and set solib-search-path differences:

  On the whole, the absolute path prefix solib-absolute-prefix setting library, valid only for absolute path; and the search path solib-search-path settings library, absolute and relative paths are working. (Compiler automatically linked so Kudrow absolute path).

  Detailed rules are:

  set solib-search-path is the path since the prefix, so that only one path is provided, and solib-search-path may be provided a plurality of search paths.

  When loading a dynamic library information Coredump encounter two routes: the absolute and relative paths. Links of compile-time libraries are often an absolute path, such as "/lib/libc.so.6","/lib/libdl.so.2" and so on, this time in Coredump file is also saved as an absolute path; and procedures dlopen function library so loaded using a relative path may, for example, "./libddd.so", save the file at this time the same path Coredump intact.

  For ease of presentation, represented by a path set solib-absolute-prefix provided by A, R & lt (A) represented by A removing path after root prefix (i.e., minus the prefix "/" symbol), with Bn represents a set solib-search-path settings each path, represented by X Coredump stored in the library path, i.e. the path the library file to be searched, F. (X) X represents a file name in the directory is removed (the last path "/" sign after the string).

  Absolute path, the search order is:

  1) A/X                       // 先添加solib-absolute-prefix前缀进行搜索,成功则不再继续,否则继续2)
  2) R(A)/X                 // 再把1)的根前缀去掉后进行搜索,成功则不再继续,否则继续3)
  3) Bn/R(A)/X         // 再在2)的基础上逐一添加solib-search-path中的每条路径进行搜索,成功则不再继续,否则继续4)
  4) Bn/F(X)               // 再只使用2)中的文件名(去掉目录段),并逐一添加solib-search-path中的每条路径进行搜索,成功则不再继续,否则继续5)
  5) $PATH/R(A)/X                                    // 在2)的基础上使用环境变量$PATH中的每条路径进行搜索,成功则不再继续,否则继续6)
  6) $LD_LIBRARY_PATH/R(A)/X         // 在2)的基础上使用环境变量$LD_LIBRARY_PATH中的每条路径进行搜索,成功则不再继续,否则继续7)
  7) 返回失败

Bloggers Note: gdb set the environment variables such as LD_LIBRARY_PATH gdb commands can be achieved by:

(gdb) set env LD_LIBRARY_PATH /tmp

For the relative path, the search order is:

  1) X                            // 直接使用原始路径进行搜索,成功则不再继续,否则继续2)
  2) Bn/X                    // 再逐一添加solib-search-path中的每条路径进行搜索,成功则不再继续,否则继续3)
  3) Bn/F(X)               // 再只使用文件名(去掉目录段),并逐一添加solib-search-path中的每条路径进行搜索,成功则不再继续,否则继续4)
  4) $PATH/X                                    // 再使用环境变量$PATH中的每条路径进行搜索,成功则不再继续,否则继续5)
  5) $LD_LIBRARY_PATH/X         // 再使用环境变量$LD_LIBRARY_PATH中的每条路径进行搜索,成功则不再继续,否则继续6)
  6) 返回失败

  ======================================================================

  for example:

  set solib-absolute-prefix /root/temp
  set solib-search-path /home/evan:/home/peter
  $PATH is /usr/sbin:/usr/bin
  $LD_LIBRARY_PATH is /opt:/usr/games

  Then the search order of absolute path "/lib/libc.so.6" are:

  1) A/X
      /root/temp/lib/libc.so.6
  2) R(A)/X
      root/temp/lib/libc.so.6
  3) Bn/R(A)/X
      /home/evan/root/temp/lib/libc.so.6
      /home/peter/root/temp/lib/lic.so.6
  4) Bn/F(X)
      /home/evan/libc.so.6
      /home/peter/libc.so.6
  5) $PATH/R(A)/X
      /usr/sbin/root/temp/lib/libc.so.6
      /usr/bin/roo/temp/lib/lic.so.6
  6) $LD_LIBRARY_PATH/R(A)/X
      /opt/root/temp/lib/libc.so.6
      /usr/games/root/temp/lib/libc.so.6

  The relative path "./libddd.so" search order is

  1) X
      ./libddd.so
  2) Bn/X
      /home/evan/./libddd.so
      /home/peter/./libddd.so
  3) Bn/F(X)
      /home/evan/libddd.so
      /home/peter/libddd.so
  4) $PATH/X
      /usr/sbin/./libddd.so
      /usr/bin/./libddd.so
  5) $LD_LIBRARY_PATH/X
      /opt/./libddd.so
      /usr/games/./libddd.so

  Seen from above, absolute and relative paths are one step is to use the file name and solib-search-path stitching to find (Step 3 Step 4 absolute path and relative path), so long as the set solib-search- set up a direct path for each library catalog file is located, then we can ensure that every library can be found.

4. Check whether the load path so the library can use the info sharedlibrary correct command, if the corresponding file has been found it From and To load address will have value, and is the address where the file is loaded on the right path display, this time, if so the library file contains symbolic information, the value syms Read Yes, otherwise No, the corresponding file is not found if the From and to address is empty, syms Read No value, appear on the right shows the path of the file Coredump in library file path.

5. If Coredump file being loaded, or when sharedlibrary info command, appears "Can not access memory at address 0x87000069" such a mistake, it is usually because the main executable file ( "file" command is used or "exec-file "command) and Coredump file (" core "command or" core-file "command) they do not match the lead. This time should check whether the main executable file is the main executable file used to generate Coredump, almost as long as it could lead to a dynamic library information read error.

6. If the loading process has "warning: .dynamic section for" /lib/librt.so.1 "is not at the expected address (wrong library or version mismatch?)" Such tips, which are usually the library search path set incorrectly, GDB loaded the wrong library file caused. In this case, use the info sharedlibrary command to view the library load path, and use the set sysroot or set solib-search-path modify the search path to the library to correct errors on the correct path.

7. After setting the search path path, it is best to use the file command to load the main executable file, and then load Coredump core command file, so as to ensure proper load symbol table library. Otherwise, if the file is first loaded into Coredump core command, and then file command to load the main executable file, it will only cause the library to be searched but not loaded symbol (using the info sharedlibrary command to see), then re-execute a core command on it.

8. A practical example of the search:

The current directory is / home

The main executable file in / home / evan / gdbso / mips / gdbso

Core file in / home / evan / gdbso / mips / Coredump

The dynamic library and copied to the same directory as the main executable file

Standard library compiled executable file used by the master is copied to the main executable file directory lib /home/evan/gdbso/mips/lib/libxxx.so

Enter the GDB, load the main executable file with the command:

evan@ubunu:/home$ mips-linux-gnu-gdb
 ...
 (gdb) file evan/gdbso/mips/gdbso
 Reading symbols from /home/evan/gdbso/mips/gdbso...done.
 (gdb) info sharedlibrary 
 No shared libraries loaded at this time.

When you can see only the main executable file loaded, it was unable to get a dynamic library information. Then the core command to load Coredump file:

(gdb) core evan/gdbso/mips/Coredump
 ...
 warning: .dynamic section for "/lib/libc.so.6" is not at the expected address (wrong library or version mismatch?)
 ...
 (gdb) info sharedlibrary 
 From        To          Syms Read   Shared Object Library
 0x2aad98c0  0x2aadd6d8  Yes         /lib/librt.so.1
 0x2aaf3460  0x2ab0db98  Yes         /lib/libm.so.6
 0x2ab7e2e0  0x2ab89b28  Yes         /lib/libpthread.so.0
 0x2abba9a0  0x2acb2bd8  Yes         /lib/libc.so.6
 0x2ad06a40  0x2ad07988  Yes         /lib/libdl.so.2
                         No          /lib/ld.so.1
                         No          ./libddd.so
 (gdb) 

After simultaneously with the main executable file and Coredump with info sharedlibrary you can see the dynamic library information. But there are tips library version does not match during the loading process. By info sharedlibrary also see the GDB incorrectly loaded the system comes with standard libraries. We will definitely set the path to a directory that does not exist take a look at the original path name saved Coredump in:

(gdb) set sysroot /noexist
 ...
 (gdb) info sharedlibrary 
 From        To          Syms Read   Shared Object Library
                         No          /lib/librt.so.1
                         No          /lib/libm.so.6
                         No          /lib/libpthread.so.0
                         No          /lib/libc.so.6
                         No          /lib/libdl.so.2
                         No          /lib/ld.so.1
                         No          ./libddd.so
 (gdb) 

Coredump saved in the original path called /lib/librt.so.1, in order to allow GDB to use the correct library /home/evan/gdbso/mips/lib/librt.so.1, just to set the absolute path prefix / home / evan / gdbso / mips can, here set to evan / gdbso / mips to demonstrate the effect of:

(gdb) set sysroot evan/gdbso/mips
 ...
 (gdb) info sharedlibrary 
 From        To          Syms Read   Shared Object Library
 0x2aad98c0  0x2aade270  Yes         evan/gdbso/mips/lib/librt.so.1
 0x2aaf3110  0x2ab31b70  Yes         evan/gdbso/mips/lib/libm.so.6
 0x2ab7e320  0x2ab8e620  Yes         evan/gdbso/mips/lib/libpthread.so.0
 0x2abba6a0  0x2accc3f0  Yes         evan/gdbso/mips/lib/libc.so.6
 0x2ad06b50  0x2ad07c70  Yes         evan/gdbso/mips/lib/libdl.so.2
 0x2aaa8810  0x2aac2e40  Yes         evan/gdbso/mips/lib/ld.so.1
                         No          ./libddd.so
 (gdb) 

You can see, GDB has been correctly loaded the absolute path. But the relative path "./libddd.so" has not been found, in order to use /home/evan/gdbso/mips/libddd.so, set the library search path contains / home / evan / gdbso / mips can be. To see the effect, there is also added a search path that does not exist:

 (gdb) set solib-search-path /noexist:/home/evan/gdbso/mips
 ...
 (gdb) info sharedlibrary 
 From        To          Syms Read   Shared Object Library
 0x2aad98c0  0x2aade270  Yes         evan/gdbso/mips/lib/librt.so.1
 0x2aaf3110  0x2ab31b70  Yes         evan/gdbso/mips/lib/libm.so.6
 0x2ab7e320  0x2ab8e620  Yes         evan/gdbso/mips/lib/libpthread.so.0
 0x2abba6a0  0x2accc3f0  Yes         evan/gdbso/mips/lib/libc.so.6
 0x2ad06b50  0x2ad07c70  Yes         evan/gdbso/mips/lib/libdl.so.2
 0x2aaa8810  0x2aac2e40  Yes         evan/gdbso/mips/lib/ld.so.1
 0x2ad1a590  0x2ad1a770  Yes         /home/evan/gdbso/mips/libddd.so
 (gdb) 

You can see that all the libraries to find the correct path, Syms has also been correctly loaded.

Published 233 original articles · won praise 106 · views 560 000 +

Guess you like

Origin blog.csdn.net/lixiangminghate/article/details/90754184