代码

#include <stdio.h>
#include <netdb.h>
int main()
{
        struct hostent *he;
        char hostname[40]={0} ;
        char ipaddr[40]={0};


        gethostname(hostname,sizeof(hostname));
        he = gethostbyname(hostname);
        printf("%d-handle-gethostbyname\n",he);
        printf("hostname=%s\n",hostname);
        //printf("%s\n",inet_ntoa(*(struct in_addr*)(he->h_addr)));
	char destIP[128];
	char **phe = NULL;
	for( phe=he->h_addr_list ; NULL != *phe ; ++phe)
	{
		inet_ntop(he->h_addrtype,*phe,destIP,sizeof(destIP));
		printf("addr:%s\n",destIP);
    
	}
}



编译
 cc -g gh.c -o gh
调试
[root@xxx ~]# gdb gh
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-32.el5_6.2)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/codeman/gh...done.
(gdb) l   //显示程序代码
1       #include <stdio.h>
2       #include <netdb.h>
3       int main()
4       {
5               struct hostent *he;
6               char hostname[40]={0} ;
7               char ipaddr[40]={0};
8
9               gethostname(hostname,sizeof(hostname));
10              he = gethostbyname(hostname);
(gdb) l     //继续显示程序代码
11              printf("%d-handle-gethostbyname\n",he);
12              printf("hostname=%s\n",hostname);
13              //printf("%s\n",inet_ntoa(*(struct in_addr*)(he->h_addr)));
14       char destIP[128];
15        char **phe = NULL;
16       for( phe=he->h_addr_list ; NULL != *phe ; ++phe){
17              inet_ntop(he->h_addrtype,*phe,destIP,sizeof(destIP));
18              printf("addr:%s\n",destIP);
19          
20          }
(gdb) l //继续显示程序代码
21      }
(gdb) l //继续显示程序代码
Line number 22 out of range; gh.c has 21 lines.
(gdb) break 1  //设置断点在第一行
Breakpoint 1 at 0x400593: file gh.c, line 1.
(gdb) break 15 //再设置断点在第15行
Breakpoint 2 at 0x400629: file gh.c, line 15.
(gdb) r //启动调试程序
Starting program: /home/codeman/gh 
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000


Breakpoint 1, main () at gh.c:6
6               char hostname[40]={0} ;
(gdb) n //现在到达断点了,继续走一行【单步】
7               char ipaddr[40]={0};
(gdb) n //再走一行【单步】
9               gethostname(hostname,sizeof(hostname));
(gdb) print hostname //打印变量 hostname
$1 = '\000' <repeats 39 times>
(gdb) n //再走一行【单步】
10              he = gethostbyname(hostname);
(gdb) print hostname //打印变量 hostname
$2 = "xxx.ooo.yyy.net", '\000' <repeats 17 times>
(gdb) bt //打印堆栈
#0  main () at gh.c:10
(gdb) c //继续往下走,到下一断点去
Continuing.
1836408896-handle-gethostbyname
hostname=xxx.ooo.yyy.net


Breakpoint 2, main () at gh.c:15
15        char **phe = NULL;
(gdb) n //现在到达15行的断点了,再走一步
16       for( phe=he->h_addr_list ; NULL != *phe ; ++phe){
(gdb) print he //打印变量
$3 = (struct hostent *) 0x356d756040
(gdb) print he->h_addr_list //打印变量
$4 = (char **) 0x601020
(gdb) print he->h_addr_list[0] //打印变量
$5 = 0x601010 "\177"
(gdb) print he->h_addr_list[1] //打印变量
$6 = 0x0
(gdb) c //继续往下走,到下一断点去,实际是跑完了
Continuing.
addr:127.0.0.1


Program exited normally.
(gdb) q //退出gdb
[root@xxx ~]# 
GitHub 加速计划 / li / linux-dash
13
2
下载
A beautiful web dashboard for Linux
最近提交(Master分支:4 个月前 )
186a802e added ecosystem file for PM2 5 年前
5def40a3 Add host customization support for the NodeJS version 5 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐