| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1252 人关注过本帖
标题:子函数在return 前好好的,返回之后反而不好是为什么?
只看楼主 加入收藏
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
收藏
已结贴  问题点数:0 回复次数:6 
子函数在return 前好好的,返回之后反而不好是为什么?
程序代码:
void exit_stub(int status)
{
      char a[]="hello world";
      printf("exit_stub %d %p\n", status, &status);
      return ;   

}

int main(int argc,char** argv)
{
      printf("\ntest generic function 1\n");
    ContainAdd((void*)(exit), (void*)(exit_stub));
    printf("\ntest generic function 2\n");
    exit_stub(1);
    printf("\ntest generic function 3\n");
    RemoveAdd((void*)exit);
    ......
上述程序执行后显示神奇的错误:我的遗憾在于:
在exit_stub(1)返回之前的 printf("exit_stub %d %p\n", status, &status); 能正确执行,为什么返回之后就错误了呢 ?
图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: 函数 return 好好的 
2010-10-22 00:02
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:20 
你用 gdb 追踪一下函数栈的状态看看调用关系(进 gdb 后用 run 运行,段错误之后用 bt 看)。怀疑是 exit_stub 中 printf 返回点不对。
2010-10-22 18:29
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
test generic function 11

test generic function 21

Breakpoint 3, exit_stub (status=1) at hooktest3.c:240
240          char a[]="hello world";
(gdb) c
Continuing.
exit_stub 1 0xbffff050, 0x80488c7

Program received signal SIGSEGV, Segmentation fault.
0x00000001 in ?? ()
(gdb) bt
#0  0x00000001 in ?? ()
#1  0xb7d41ace in __libc_start_main () from /lib/libc.so.6
#2  0x08048491 in _start () at ../sysdeps/i386/elf/start.S:119
(gdb)
这个是失败后,使用bt显示的信息,怎么有个  0x00000001 in ?? () 的函数地址这么小呢 ?


[ 本帖最后由 vfdff 于 2010-10-24 02:37 编辑 ]

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-24 02:36
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
不清楚,怎么会连 main 函数都没有呀,难得段错误是在 main 函数返回之后才收到的?或者是程序在执行的过程中破坏栈的结构了?
看来你得慢点跟踪了,看看是什么问题。
2010-10-24 17:25
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
这里有 _start (),好像_start ()是里会调用main的吧


(gdb) disas exit
Dump of assembler code for function exit@plt:
0x08048458 <exit@plt+0>:    jmp    *0x804a018
0x0804845e <exit@plt+6>:    push   $0x30
0x08048463 <exit@plt+11>:    jmp    0x80483e8
End of assembler dump.
另外,以上的反汇编能不能说明这个exit函数占用 6 + 11 = 17个字节吧?
(从 jmp    *0x804a018看出跳转指令需要 6 个字节,因此也假设jmp    0x80483e8需要6个字节)


[ 本帖最后由 vfdff 于 2010-10-24 23:37 编辑 ]

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-24 23:16
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
_start 和 __libc_start_main 都是做调 main 之前的准备工作的,之后会调的。但是你那里 main 没出现在栈里,这很奇怪。

你给的那段代码是动态调用库时用的。他跳过去的地址处可能是 exit 的这个函数,那么就会执行 exit。如果不是,那么会加载一次 exit。
调用所有共享库的代码都是这样的,从这三行里看不出调的那个函数有什么功能。
2010-10-24 23:54
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
HCC:/home/zyd/projects # valgrind --tool=memcheck --leak-check=full --show-reachable=yes  ./test
==24939== Memcheck, a memory error detector.
==24939== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==24939== Using LibVEX rev 1884, a library for dynamic binary translation.
==24939== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==24939== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==24939== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==24939== For more details, rerun with: -v
==24939==
--24939-- DWARF2 CFI reader: unhandled CFI instruction 0:10
--24939-- DWARF2 CFI reader: unhandled CFI instruction 0:10

test generic function 11

test generic function 21
exit_stub 1 0xbe8eb050, 0x80488c7
==24939==
==24939== Process terminating with default action of signal 11 (SIGSEGV)
==24939==  Access not within mapped region at address 0x0
==24939==    at 0x1: ???
==24939==    by 0x4196ACD: (below main) (in /lib/libc-2.10.1.so)
==24939==  If you believe this happened as a result of a stack overflow in your
==24939==  program's main thread (unlikely but possible), you can try to increase
==24939==  the size of the main thread stack using the --main-stacksize= flag.
==24939==  The main thread stack size used in this run was 8388608.
==24939==
==24939== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
==24939== malloc/free: in use at exit: 0 bytes in 0 blocks.
==24939== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==24939== For counts of detected errors, rerun with: -v
==24939== All heap blocks were freed -- no leaks are possible.
Segmentation fault
HCC:/home/zyd/projects #

刚才使用valgrind工具打印出一些更详细的信息,但是原因我也还在做进一步分析,如果您有什么新的想法,也希望您能及时指教

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-25 00:20
快速回复:子函数在return 前好好的,返回之后反而不好是为什么?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038900 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved