But inet_ntoa () does not accept the u_int32_t argument, but rather the struct in_addr argument, so the previous answer: p (char *) inet_ntoa (3) seems wrong to me.
Here is a way to define a function in a file called gdb.cmd.txt, so "gdb -x gdb.cmd.txt" is called at startup.
In 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\n", $ipv4, $val1, $val2, $val3, $val4, $val1, $val2, $val3, $val4 end
Then run gdb and call ntoa "user 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
code>
BTW: now I'm looking for a way in gdb that the function returns a formatted string so that I can run the command "p ntoa (0x01020304)" or "p ntoa (ptr-> ipv4_addresss)" (assuming ptr is a valid ptr for structure containing the u_int32_t ipv4_address data item). But it turns out that user-defined gdb functions do not allow sprintf () calls.
-dennis bednar Dec 2012
source share