Basic (26) GDB Debugging so tutorial

Source: Internet
Author: User

When you develop a so file, how to debug it?

For a and how to write a so, please refer to the article: http://blog.csdn.net/zz7zz7zz/article/details/41448987

For GDB debugging, please refer to the article: http://blog.csdn.net/zz7zz7zz/article/details/41654457


Let's say our source files are:

Filea.c

#include <stdio.h>void fun1 () {printf ("I am from Filea fun1 \ n");p rintf ("I am from Filea fun11 \ n");p rintf ("I am from Filea fun12 \ n ");}

Fileb.c

#include <stdio.h>void fun2 () {printf ("I am from Fileb fun2 \ n");p rintf ("I am from Fileb fun21 \ n");p rintf ("I am from Fileb fun22 \ n ");}

CLoadSo.h

#ifndef _cloadso_h#define _cloadso_h#ifdef _cplusplusextern "C" {#endifvoid fun1 (); void Fun2 (); #ifdef _cplusplus}# Endif#endif

Cloadso.c

#include <stdio.h> #include <dlfcn.h> #include "CLoadSo.h" int main (int argc,char **argv) {void *sohandle;int (*fun) (); Char *errormsg;sohandle=dlopen ("first.so", Rtld_lazy), Errormsg=dlerror ();p rintf ("A1---------Loadso is%s \ n", Sohandle? "Success": "Failed"), if (errormsg) {printf ("A2--------loadso Error, MSG is:%s \ n", errormsg); return-1;} Fun=dlsym (Sohandle, "fun1"); Errormsg=dlerror ();p rintf ("B1---------fun1, fun1 is%s \ n", fun? "Success": "Null"), if (fun) {fun ();} if (errormsg) {printf ("B2---------fun1 Error, MSG is:%s \ n", errormsg);} Fun=dlsym (Sohandle, "fun2"); Errormsg=dlerror ();p rintf ("B3---------fun2, fun2 is%s \ n", fun? "Success": "Null"), if (fun) {fun ();} if (errormsg) {printf ("B4---------fun2 Error, MSG is:%s \ n", errormsg);} Fun=dlsym (Sohandle, "fun3"); Errormsg=dlerror ();p rintf ("B5---------Fun3, Fun3 is%s \ n", fun? "Success": "Null"), if (fun) {fun ();} if (!errormsg) {printf ("B6---------fun3 Error, MSG is:%s \ n", errormsg);} Dlclose (Sohandle);p rintf ("C---------close LOADSO \ n"); return 0;} 

With the command gcc-wall-fpic-g-shared filea.c fileb.c-o first.soGenerate First.so File

Generate the executable file with the command gcc-g-wall cloadso.c-o cloadso-ldl cloadso


The GDB steps are as follows (please observe the Red Comment statement):


[email protected] gdbso]$ gdb CLOADSO ----------(1). Start GdB Debugging

GNU gdb (GDB) CentOS (7.0.1-45.el5.centos) Copyright (C) software Foundation, Inc.license gplv3+: GNU GPL version 3 or later 
The result: successful entry into the fun1 of so

Approximate steps:

1. Start GdB debugging: GdB CLOADSO

2. Breaking point gdb): Break 28

3. Execute gdb): Run

4. read the symbol for the dynamic library into GDB for you to find the variable and function name gdb) sharedlibrary first.so

5.s Enter function, l list source code, n step to end.









Basic (26) GDB Debugging so tutorial

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.