English 中文(简体)
printing ip addresses using gdb
原标题:

I am debugging a networking code and want to print ip addresses which are declared as int32. when i print it using gdb print command, i get some values which is not much meaningful.

How can i possibly print them in meaningful format?

最佳回答

Just use inet_ntoa(3) as so:

(gdb) p (char*)inet_ntoa(0x01234567)  # Replace with your IP address
$1 = 0xa000b660 "103.69.35.1"
问题回答

If you are debugging a core file and can t use inet_ntoa() then you can also do something like:

(gdb) set $int_ip = 0x01234567
(gdb) printf "%d.%d.%d.%d
", ($int_ip & 0xff), ($int_ip >> 8) & 0xff, ($int_ip >> 16) & 0xff, ($int_ip >> 24)
103.69.35.1
(gdb) 

But inet_ntoa() doesn t take a u_int32_t argument, but rather a struct in_addr argument, so the prior answer: p (char *)inet_ntoa(3) seems wrong to me.

Heres a way to define a function in a file named gdb.cmd.txt, so invoke "gdb -x gdb.cmd.txt" at startup.

In the gdb.cmd.txt, put this:

define ntoa
        set $ipv4 = $arg0
        echo IPV4 =.
        p $ipv4
        set $val1 = ($ipv4 >> 24) & 0xff
        set $val2 = ($ipv4 >> 16) & 0xff
        set $val3 = ($ipv4 >>  8) & 0xff
        set $val4 = ($ipv4 >>  0) & 0xff
       printf "IPV4=%u=0x%02x.%02x.%02x.%02x =%d.%d.%d.%d
", $ipv4, $val1, $val2, $val3, $val4, $val1, $val2, $val3, $val4
    end

Then run gdb, and call the ntoa "user defined function" as follows:

(gdb) ntoa(0x01020304)
IPV4 =.$10 = 16909060
IPV4=16909060=0x01.02.03.04 =1.2.3.4
(gdb) ntoa(-1)
IPV4 =.$10 = -1
IPV4=4294967295=0xff.ff.ff.ff =255.255.255.255

BTW: I am now searching if there is way in gdb to have a function return a formated string, so that I can run the command "p ntoa(0x01020304)" or "p ntoa(ptr->ipv4_addresss)" (assuming ptr is a valid ptr to a structure containing a u_int32_t ipv4_address" data element). But it appears that gdb user defined functions don t allow sprintf() calls.

-dennis bednar Dec 2012

Explaining Gopinath s response:

(gdb) p/uc (char[4]) 342757386
$4 = {10  
 , 16  20 , 110  n , 20  24 }

p/uc: tells gdb to treat the data as unsigned char content.

(char[4]) 342757386: casts the IP to an array of 4 char elements, each one representing one byte/octet.

So you re telling gdb to treat the decimal represention of the IP as an array of four bytes - the four octets - and then print them as unsigned chars.

If you ignore the ASCII representation of each byte, you have your IP address: 10.16.110.20.

Make a function that calls inet_ntoa, and then call it with the p command in gdb on your int.

Here s another interesting way:

#include <sys/types.h>  // u_int32_t


// next 3 for inet_ntoa() call
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

// C++ headers
#include <iostream> // std::cout
#include <string>
#include <sstream>  // ostringstream


// class to aid in using the gdb(3) debugger to print a u_int32_t ip_address as a string.
// The toString() is a static method!!
// How to call a C++ method from the gdb debugger, to simulate the inet_nto(3) method
//
// From within gdb debugger, you must have a process, so first stop at main:
//  b main
//  r
//
//  (gdb) p MYIP4::toString(0x0fffefdfc)
//  $1 = "255.254.253.252"
//
//  (gdb) p MYIP4::toString(-1)
//  $2 = "255.255.255.255"
//
//  (gdb) p MYIP4::toString(65536)
//  $3 = "0.1.0.0"
//
// Drawbacks: the a.out executable that you are debugging needs the MYIP4 class already
// compiled and linked into the executable that you are debugging.
//
// PS: I don t know if there is a "slick way" where the MyIP4 class could dynamically be loaded,
// within gdb(), if the executable failed to contain the MYIP4 class.
//
// PS: I had trouble with my operator() idea.. If you know how to improve on it, post away!
//
// @author 1201207 dpb  created
//=============================

class MYIP4
{
public:
    static std::string toString(u_int32_t ipv4_address )
    {
        struct in_addr temp_addr;

        // inet_ntoa() returns a char * to a buffer which may be overwritten
        // soon, so convert char* to a string for extra safety.
        temp_addr.s_addr = htonl(ipv4_address);
        std::string ipv4String = std::string(inet_ntoa( temp_addr ));
        return ipv4String;
    }

#if 0
    // this idea did NOT work, so it is commented out.
    //
    // overload the () operator, so that, within gdb, we can simply type:
    //  p MYIP4(0x01020304)
    // instead of:
    //  p MYIP4::toString(0x01020304)

    std::string operator() ( u_int32_t ipv4_address )
    {
        return toString(ipv4_address);
    }
#endif

};


void test1();

int main()
{
    test1();
    return 0;
}

void test1()
{
    u_int32_t   ipv4Address = 0x01020304;   // host byte order for 1.2.3.4
    std::cout << "Test1: IPAddress=" << MYIP4::toString(ipv4Address) << "
";
}

Try this command:

(gdb) p/uc (char[4])342757386
$1 = {10  
 , 16  20 , 110  n , 20  24 }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?