Debugging the so dynamic library with GDB on Linux

Source: Internet
Author: User

I have written a lot of programs on the Linux platform, but I am not very familiar with debugging. I learned a little about it over the past few days and made some debugging on so. First, two source files are provided, namely the source code of the calling program and the called dynamic library program. Source code of the calling program: Main. cpp

# Include <stdio. h> # include <dlfcn. h> # include <link. h> # include <stdlib. h> int main () {void * DLH = NULL; int (* printmsg) (char * PMSG); int result; printf ("Load Library! /R/N "); DLH = dlopen ("./libprint. So ", rtld_lazy); If (DLH = NULL) {printf (" Load Library fail! "); Return-1;} printf (" search function! /R/N "); printmsg = (INT (*) (char *) dlsym (DLH," dj_print "); If (printmsg = NULL) {printf ("search function fail! "); Dlclose (DLH); DLH = NULL; Return-2;} printf (" call function.../R/N "); Result = printmsg (" libarcomm succeed! /R/N "); printf (" Call result % d./R/N ", result); dlclose (DLH); Return 0 ;}

Source code of the called dynamic library program: libprint. cpp

# Ifdef _ cplusplusextern "C" {# endif # include <stdio. h> int dj_print (char * pbuffer) {If (pbuffer = NULL) {return-1;} return printf ("% s", pbuffer );} # ifdef _ cplusplus} # endif

Then compile them. No makefile is required. Next time, write down how to write the MAKEFILE file.

G ++-ggdb3./loadso. cpp-LDL-shared

G ++-LDL-wall-shared-ggdb3-FPIC-O libprint. So./libprint. cpp

After compilation, a file a. Out and libprint. So will be generated. Then run

[DJ @ dj_linux sodemo] $ GDB./A. Out

Input l will list the source code of the current location as follows:

(GDB) L1 # include <stdio. h> 2 # include <dlfcn. h> 3 # include <link. h> 4 # include <stdlib. h> 56 int main () 7 {8 void * DLH = NULL; 9 int (* printmsg) (char * PMSG); 10 int result;

If you want to pause the dj_print function in so, enter the following command:

(GDB) Break dj_print

Function "dj_print" not defined. Make breakpoint pending on future shared library load? (Y or [N]) ybreakpoint 1 (dj_print) pending.

The following prompt indicates whether to set a breakpoint when calling a dynamic library. Select y. Then enter run

(GDB) runstarting program:/home/DJ/work/src/sodemo/a. out load library! Breakpoint 2 at 0xe085e7: file./libprint. cpp, line 11. Pending breakpoint "dj_print" resolvedsearch function! Call function... breakpoint 2, dj_print (pbuffer = 0x804256e "libarcomm succeed! /R/N ") at./libprint. cpp: 1111 if (pbuffer = NULL)

This will be paused when dj_print is set. You can also use the LIST command to view the code to be executed.

(GDB) L6 # include <stdio. h> 7 # include "libprint. H "89 int dj_print (char * pbuffer) 10 {11 if (pbuffer = NULL) 12 {13 return-1; 14} 15 return printf (" % s ", pbuffer );

Then you can debug it step by step. Note that when the source code is not in the same directory as the executable program, you can add the source code directory to the directory found by GDB. The command is as follows:

Show DIR Displays the directory currently queried

The command above dir <source program directory> adds the source program directory to the current search directory. You can run this command multiple times to add multiple search directories. The details are as follows:

(GDB) dir. Source directories searched:/home/DJ/work/src/sodemo: $ cdir: $ CWD

If you enter dir directly, press enter to clear the search directory you added earlier.

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.