imprimer des adresses ip en utilisant gdb
-
08-07-2019 - |
Question
Je suis en train de déboguer un code réseau et je souhaite imprimer les adresses IP déclarées comme int32
.
Quand je l'imprime en utilisant la commande d'impression gdb, j'obtiens des valeurs qui n'ont pas beaucoup de sens.
Comment puis-je les imprimer dans un format significatif?
La solution
Utilisez simplement inet_ntoa (3)
comme suit:
(gdb) p (char*)inet_ntoa(0x01234567) # Replace with your IP address
$1 = 0xa000b660 "103.69.35.1"
Autres conseils
Si vous êtes en train de déboguer un fichier core et que vous ne pouvez pas utiliser inet_ntoa (), vous pouvez aussi faire quelque chose comme:
(gdb) set $int_ip = 0x01234567 (gdb) printf "%d.%d.%d.%d\n", ($int_ip & 0xff), ($int_ip >> 8) & 0xff, ($int_ip >> 16) & 0xff, ($int_ip >> 24) 103.69.35.1 (gdb)
Mais inet_ntoa () ne prend pas un argument u_int32_t, mais plutôt un argument struct in_addr, ainsi la réponse précédente: p (char *) inet_ntoa (3) semble faux pour moi.
Voici un moyen de définir une fonction dans un fichier nommé gdb.cmd.txt. Vous devez donc appeler "gdb -x gdb.cmd.txt". au démarrage.
Dans le fichier gdb.cmd.txt, mettez ceci:
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
Ensuite, lancez gdb et appelez le ntoa "fonction définie par l'utilisateur". comme suit:
(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: Je cherche maintenant s’il est possible dans gdb de renvoyer une chaîne formatée par une fonction, afin de pouvoir exécuter la commande "p ntoa (0x01020304)". ou "p ntoa (ptr- > ipv4_addresss)" (en supposant que ptr est un ptr valide pour une structure contenant un élément de données u_int32_t ipv4_address "). Mais il semble que les fonctions définies par l'utilisateur gdb n'autorisent pas les appels sprintf ().
-dennis bednar déc 2012
Créez une fonction qui appelle inet_ntoa, puis appelez-la avec la commande 'p' dans gdb sur votre int.
Expliquer la réponse de Gopinath:
(gdb) p/uc (char[4]) 342757386
$4 = {10 '\n', 16 '\020', 110 'n', 20 '\024'}
p / uc
: indique à gdb de traiter les données en tant que contenu de caractère non signé.
(char [4]) 342757386
: convertit l'adresse IP en un tableau de 4 éléments char, chacun représentant un octet / octet.
Vous demandez donc à gdb de traiter la représentation décimale de l'IP comme un tableau de quatre octets - les quatre octets - et de les imprimer ensuite comme des caractères non signés.
Si vous ignorez la représentation ASCII de chaque octet, vous avez votre adresse IP: 10.16.110.20
.
Voici un autre moyen intéressant:
#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) << "\n";
}
Essayez cette commande:
(gdb) p/uc (char[4])342757386
$1 = {10 '\n', 16 '\020', 110 'n', 20 '\024'}