每天学点GDB14
在上一篇文章中讲到了ptrace,那么我们完全可以用ptrace来写一个非常简单的trace工具,用以trace程序的具体运行过程。
用它可以很清楚的回答,使用glibc编译后的hello world是从什么地方开始运行的。
(注:本文内容根据“A really simple tracing debugger"翻译而来,具体链接见参考资料一节)
itrace.c
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h> #include <sys/user.h>
#include <sys/ptrace.h> int main(int argc, char **argv)
{
int pid = fork();
if(pid == ) {
if(ptrace(PTRACE_TRACEME) < ) {
perror("ptrace");
_exit();
}
execvp(argv[], argv + );
perror("exec");
_exit();
}
while() {
int status;
struct user_regs_struct regs;
if(waitpid(pid, &status, ) < )
perror("waitpid");
if(!WIFSTOPPED(status))
break;
if(ptrace(PTRACE_GETREGS, pid, , ®s) < )
perror("ptrace/GETREGS");
printf("%lx %lx\n", regs.eip, regs.esp);
if(ptrace(PTRACE_SINGLESTEP, pid, , ) < )
perror("ptrace/SINGLESTEP");
}
return ;
}
编译
gcc -m32 itrace.c -o itrace
译者注: -m32表示编译成32位格式,如果是在64位机器上,不需要加此选项,同时将itrace.c源码中的eip和esp转换为rip,rsp.
hellow.c
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return ;
}
编译
gcc -static -o hellow hellow.c
译者注: itrace保持一致,itrace如果是按32位格式来编译的,此处也应该一样。
测试运行
./itrace ./hellow | addr2line -e ./hellow -f | grep -v "??\|:?" | uniq
说明如下
addr2line 是将地址转换为相应的源码
运行的结果很长,所以就不打算没有贴出来了。
treeify
为了让运行结果在显示的时候能够更好的反映出调用关系,根据堆栈(%esp)中的信息采用python脚本将其层次化的打印出来。
import subprocess
import sys def read():
for line in sys.stdin:
try:
regs = [int(x, 16) for x in line.split(" ")]
yield {"eip": regs[0], "esp": regs[1]}
# Ignore lines interspersed with other output!
except (ValueError, IndexError):
pass def addr2line(iterable):
proc = subprocess.Popen(["addr2line", "-e", sys.argv[1], "-f"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for regs in iterable:
proc.stdin.write("%x\n" % regs["eip"])
a = proc.stdout.readline().rstrip("\n")
b = proc.stdout.readline().rstrip("\n")
regs["func"] = "%s %s" % (a, b)
yield regs def entry_points(iterable):
funcs = {}
# We treat the first address we see for the function as its entry
# point, and only report those entries from this point on.
for regs in iterable:
func = regs["func"].split(":")[0]
if funcs.setdefault(func, regs["eip"]) == regs["eip"]:
yield regs def add_nesting(iterable):
stack = [2 ** 64]
for regs in iterable:
stack_pos = regs["esp"]
if stack_pos < stack[-1]:
stack.append(stack_pos)
while stack_pos > stack[-1]:
stack.pop()
regs["indent"] = " " * len(stack)
yield regs for x in add_nesting(entry_points(addr2line(read()))):
print x["indent"], x["func"], "%x" % x["eip"]
运行
./itrace ./hellow|python2 ./treeify.py ./hellow
测试结果
_start /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/start.S: 8048d40
__libc_start_main ??:? 8048ea0
_dl_aux_init ??:? 806e590
_dl_discover_osversion ??:? 806f3b0
uname ??:? 80921c0
?? ??: b77e0414
index ??:? 805b250
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
__init_cpu_features ??:? 806f570
strncasecmp_l ??:? 80b5ac0
strcmp ??:? 805b460
memset ??:? 805ba70
strcasecmp_l ??:? 805bc10
bcmp ??:? 805b6b0
strstr ??:? 806b080
memchr ??:? 808ce90
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
strrchr ??:? 808c660
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
wcslen ??:? 808eae0
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
__rawmemchr ??:? 805bcc0
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
memmove ??:? 805b9d0
__strnlen ??:? 808c620
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
strcpy ??:? 805b4d0
stpcpy ??:? 805bb30
__pthread_initialize_minimal ??:? 80494d0
__libc_setup_tls ??:?
sbrk ??:? 806cd70
__brk ??:? 80928e0
?? ??: b77e0414
__brk ??:? 80928e0
?? ??: b77e0414
memcpy ??:? 805bc50
__libc_init_first ??:? 806f4b0
__setfpucw ??:? 807ac00
__libc_init_secure ??:? 806f360
_dl_non_dynamic_init ??:? 806e7f0
_dl_get_origin ??:? 809a800
?? ??: b77e0414
malloc ??:? 8058f60
malloc_hook_ini malloc.o:? 805a020
ptmalloc_init.part. malloc.o:? 8059c20
__linkin_atfork ??:? 806e0f0
malloc ??:? 8058f60
_int_malloc malloc.o:?
malloc_consolidate malloc.o:? 80560b0
malloc_init_state malloc.o:? 80552e0
__default_morecore ??:? 805b230
sbrk ??:? 806cd70
__brk ??:? 80928e0
?? ??: b77e0414
__default_morecore ??:? 805b230
sbrk ??:? 806cd70
__brk ??:? 80928e0
?? ??: b77e0414
mempcpy ??:? 805bb00
getenv ??:? 804e240
strlen ??:? 805b5f0
_dl_new_object ??:? 80972a0
strlen ??:? 805b5f0
__calloc ??:?
_int_malloc malloc.o:?
__memset_sse2_rep ??:? 805f690
memcpy ??:? 805bc50
_dl_setup_hash ??:?
strlen ??:? 805b5f0
malloc ??:? 8058f60
_int_malloc malloc.o:?
memcpy ??:? 805bc50
_dl_add_to_namespace_list ??:?
getenv ??:? 804e240
strlen ??:? 805b5f0
_dl_init_paths ??:? 80951f0
_dl_important_hwcaps ??:? 80991b0
malloc ??:? 8058f60
_int_malloc malloc.o:?
mempcpy ??:? 805bb00
malloc ??:? 8058f60
_int_malloc malloc.o:?
malloc ??:? 8058f60
_int_malloc malloc.o:?
getenv ??:? 804e240
strlen ??:? 805b5f0
getenv ??:? 804e240
strlen ??:? 805b5f0
getenv ??:? 804e240
strlen ??:? 805b5f0
getenv ??:? 804e240
strlen ??:? 805b5f0
getenv ??:? 804e240
strlen ??:? 805b5f0
__init_misc ??:? 806dd90
__strrchr_sse2_bsf ??:? 808d830
__ctype_init ??:? 807abb0
__cxa_atexit ??:? 804e5d0
__new_exitfn ??:? 804e440
__libc_csu_init ??:? 80494f0
_init /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S:
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
_init /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crtn.S: 80481ae
frame_dummy crtstuff.c:? 8048e20
__register_frame_info_bases ??:? 80bd5a0
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
register_tm_clones crtstuff.c:? 8048db0
init_cacheinfo cacheinfo.o:? 8048b10
handle_intel cacheinfo.o:? 806b380
intel_check_word cacheinfo.o:? 806b0b0
intel_check_word cacheinfo.o:? 806b0b0
intel_check_word cacheinfo.o:? 806b0b0
intel_check_word cacheinfo.o:? 806b0b0
handle_intel cacheinfo.o:? 806b380
intel_check_word cacheinfo.o:? 806b0b0
intel_check_word cacheinfo.o:? 806b0b0
_setjmp ??:? 804d970
main ??:? 8048e7c
puts ??:? 804f350
strlen ??:? 805b5f0
_IO_new_file_xsputn ??:?
_IO_file_overflow ??:? 8052e60
_IO_doallocbuf ??:? 8053b10
_IO_file_doallocate ??:? 808a730
_IO_file_stat ??:? 8051f20
___fxstat64 ??:? 806c300
?? ??: b77e0414
__mmap ??:? 806ce60
?? ??: b77e0414
_IO_setb ??:? 8053aa0
_IO_new_do_write ??:? 80527b0
_IO_default_xsputn ??:? 8053bc0
exit ??:? 804e420
__run_exit_handlers ??:? 804e320
__libc_csu_fini ??:?
fini sdlerror.o:? 8048b00
check_free.isra. sdlerror.o:? 80a6f30
__do_global_dtors_aux crtstuff.c:? 8048df0
deregister_tm_clones crtstuff.c:? 8048d80
__deregister_frame_info_bases ??:? 80bd7c0
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
_fini /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 80bec78
__x86.get_pc_thunk.bx /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crti.S: 8048d70
_fini /root/glibc/src/glibc-2.18/csu/../sysdeps/i386/crtn.S: 80bec87
_IO_cleanup ??:? 80543b0
_IO_flush_all_lockp ??:?
_IO_file_overflow ??:? 8052e60
_IO_new_do_write ??:? 80527b0
new_do_write fileops.o:?
_IO_file_write ??:? 8051f50
__write ??:? 806c420
?? ??: b77e0414
_IO_file_setbuf ??:?
_IO_default_setbuf ??:? 8053d10
_IO_file_sync ??:?
_IO_setb ??:? 8053aa0
_Exit ??:? 806ba24
?? ??: b77e0414
参考资料
每天学点GDB14的更多相关文章
- 【Python五篇慢慢弹】快速上手学python
快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...
- 跟Unity3D学代码优化
今天我们来聊聊如何跟Unity学代码优化,准确地说,是通过学习Unity的IL2CPP技术的优化策略,应用到我们的日常逻辑开发中. 做过Unity开发的同学想必对IL2CPP都很清楚,简单地说,IL2 ...
- 重学hadoop技术
最近因为做了些和hadoop相关的项目(虽然主要是运维),但是这段经历让我对hadoop的实际运用有了更加深入的理解. 相比以前自学hadoop,因为没有实战场景以及良好的大数据学习氛围,现在回顾下的 ...
- 《学技术练英语》PPT分享
之前做的一个PPT,分享给博客园的同学. 下载地址: 学技术练英语.pdf 技术是靠自己去学的,学技术不能仅仅是看书看博客,最好是有实践,不管是做实验去验证,还是写各种代码去玩各种特性,还是造轮子都是 ...
- 前端学HTTP之数据传输
× 目录 [1]客户机处理 [2]集线器处理 [3]路由器1处理[4]路由器2处理[5]交换机处理[6]服务器处理[7]反向传输 前面的话 上一篇中,介绍了网络基础.本文将详细介绍客户机在浏览网页ab ...
- 前端学HTTP之网络基础
× 目录 [1]网络 [2]OSI [3]TCP/IP 前面的话 HTTP协议对于前端工程师是非常重要的.我们在浏览网站时,访问的每一个WEB页面都需要使用HTTP协议实现.如果不了解HTTP协议,就 ...
- Mina、Netty、Twisted一起学(八):HTTP服务器
HTTP协议应该是目前使用最多的应用层协议了,用浏览器打开一个网站就是使用HTTP协议进行数据传输. HTTP协议也是基于TCP协议,所以也有服务器和客户端.HTTP客户端一般是浏览器,当然还有可能是 ...
- 一起学微软Power BI系列-使用技巧(2)连接Excel数据源错误解决方法
上一篇文章一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库中,我们介绍了Power BI Desktop中连接Oracle和Mysql的方法,其实说到底还是驱动的问题, ...
- 开始学nodejs —— 调试篇
新学习一种技术,肯定会遇到很多坑,我们需要找到这些坑,弄清楚这些坑出现的原因和其中的原理.这种操作就叫做调试. 程序调试的方法和工具多种多样,在这里我总结一下我在学习nodejs的过程中,学到的和用到 ...
随机推荐
- Spring源码下载地址
今天想下载一下Spring的源代码,登录到Spring官网,傻眼了,根本找不到下载的地方!费了九牛二虎之力在网上找到了一个下载地址,记下来,免得下次再次傻找. http://s3.amazonaws. ...
- MVC学习笔记---MVC生命周期
Asp.net应用程序管道处理用户请求时特别强调"时机",对Asp.net生命周期的了解多少直接影响我们写页面和控件的效率.因此在2007年和2008年我在这个话题上各写了一篇文章 ...
- JUC回顾之-Semaphore底层实现和原理
1.控制并发线程数的Semaphore Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,保证合理的使用公共资源. 线程可以通过acquire()方法来获取信号量的 ...
- ***Redis hash是一个string类型的field和value的映射表.它的添加、删除操作都是O(1)(平均)。hash特别适合用于存储对象
http://redis.readthedocs.org/en/latest/hash/hset.html HSET HSET key field value (存一个对象的时候key存) 将哈希 ...
- hrbustoj 1161:Leyni(树状数组练习)
LeyniTime Limit: 3000 MS Memory Limit: 65536 KTotal Submit: 260(59 users) Total Accepted: 80(55 user ...
- Java Hour 10
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为10 Hour,请各位不吝赐教. Hour 10 ...
- .NET Framework 4.6的新东西
我们知道.NET Framework 4.6即将随着Visual Studio 2015一同到来,目前依然是预览版.4.6和4,4.5,4.5.1和4.5.2是兼容的,也即安装4.6后会升级替代他们. ...
- To be transfered
bomb: file format elf64-x86-64 Disassembly of section .init: 0000000000400b38 <_init>: 400b38: ...
- isnull的使用方法
is null 查看列数据为空 select*from lrb where lrid is null ISNULL使用指定的替换值替换 NULL. 语法ISNULL ( check_express ...
- Python开发的10个小贴士
下面是十个Python中很有用的贴士和技巧.其中一些是初学这门语言常常会犯的错误. 注意:假设我们都用的是Python 3 1. 列表推导式 你有一个list:bag = [1, 2, 3, 4, 5 ...