|
From: Shujunjun <shu...@hu...> - 2015-04-02 02:36:53
|
Thank you for your suggestion.
I rewrite my code, remove the "sprinf" and "write" but the problem is still exist.
I use gdb+vgdb to trace it. It seems had same problem I said before.
In prog.c when call "malloc" for "addr1", the wrapper function run as follow:
break point 1
break point 2
break point 1
break point 2
break point3
break point3
but when call "malloc" for "addr2", the wrapper function run as follow:
break point 1
break point 2
break point3
From the backtrace information, it can see that at the first time _vgw00000ZZ_libcZdsoZd6_malloc call itself.
######in test.wrap.sub.c
#include <stdio.h>
#include "valgrind.h"
int catch_before = 0;
int catch_after = 0;
long I_WRAP_SONAME_FNNAME_ZZ(libcZdsoZd6, malloc)( long x )
{
int result = 0;
OrigFn fn;
catch_before += 1; //// break point 1
VALGRIND_GET_ORIG_FN(fn);
CALL_FN_W_W(result, fn, x); //// break point 2
catch_after += 3; //// break point3
result += 1;
return result ;
}
#### in prog.c
#include <stdio.h>
#include <stdlib.h>
extern int catch_before;
extern int catch_after;
int test2()
{ int * addr1 = 0;
int * addr2 = 0;
addr1 = (int *)malloc( 10 * sizeof(int));
addr2 = (int *)malloc( 10 * sizeof(int));
return (int)addr1 * (int)addr2;
}
int main()
{
return test2();
}
####### information of running gdb+vgdb
Breakpoint 1, 0x0000000000400928 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x0000000000400928 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x000000000040059f in test2 ()
#2 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 2, 0x0000000000400961 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x0000000000400961 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x000000000040059f in test2 ()
#2 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 1, 0x0000000000400928 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x0000000000400928 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x00000000004009af in _vgw00000ZZ_libcZdsoZd6_malloc ()
#2 0x000000000040059f in test2 ()
#3 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 2, 0x0000000000400961 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x0000000000400961 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x00000000004009af in _vgw00000ZZ_libcZdsoZd6_malloc ()
#2 0x000000000040059f in test2 ()
#3 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 3, 0x00000000004009c0 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x00000000004009c0 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x00000000004009af in _vgw00000ZZ_libcZdsoZd6_malloc ()
#2 0x000000000040059f in test2 ()
#3 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 3, 0x00000000004009c0 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x00000000004009c0 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x000000000040059f in test2 ()
#2 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 1, 0x0000000000400928 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x0000000000400928 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x00000000004005ad in test2 ()
#2 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 2, 0x0000000000400961 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x0000000000400961 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x00000000004005ad in test2 ()
#2 0x00000000004006da in main ()
(gdb) c
Continuing.
Breakpoint 3, 0x00000000004009c0 in _vgw00000ZZ_libcZdsoZd6_malloc ()
(gdb) bt
#0 0x00000000004009c0 in _vgw00000ZZ_libcZdsoZd6_malloc ()
#1 0x00000000004005ad in test2 ()
#2 0x00000000004006da in main ()
shu...@hu...
Best Regards.
________________________________________
发件人: Philippe Waroquiers [phi...@sk...]
发送时间: 2015年4月2日 3:32
收件人: Shujunjun
抄送: val...@li...
主题: Re: [Valgrind-developers] Bugs when wrap "malloc" called at the first time.
You should get the original fn before doing anything else, to ensure
to follow the following user manual paragraph:
"VALGRIND_GET_ORIG_FN: once in the wrapper, the first priority is to
get hold of the address of the original (and any other supporting
information needed). This is stored in a value of opaque type OrigFn.
The information is acquired using VALGRIND_GET_ORIG_FN. It is crucial
to make this macro call before calling any other wrapped function in
the same thread."
You might e.g. imagine that sprintf is calling malloc to do its
work.
Unknown result might happen if you do not follow the above.
Once the above is done, you might use gdb+vgdb and debug your wrappers
when running under valgrind to e.g. see who is calling them when and why.
Philippe
On Wed, 2015-04-01 at 08:25 +0000, Shujunjun wrote:
> Hi,
> I use I_WRAP_SONAME_FNNAME_ZZ to wrap “malloc”,it's quite strange when
> the guest program call "malloc" at the first time.
> The first time "malloc" called in the "prog.c", it seems to call the
> "//block 1" code twice and call the "//block 2" twice in
> "test.wrap.sub.c".
> But the second "malloc" called in the "prog.c", it seems normally.
>
>
>
> PS. It's only appeared at shared library.
>
>
>
> Following is my test code and output.
>
>
> ######in test.wrap.sub.c
> #include <stdio.h>
> #include "valgrind.h"
> int catch_before = 0;
> int catch_after = 0;
> long I_WRAP_SONAME_FNNAME_ZZ(libcZdsoZd6, malloc)( long x )
> {
> int result = 0;
> char buffer[60];
> OrigFn fn;
>
> ////////////block 1
> catch_before += 1;
> sprintf(buffer, "----------- 111 wrap malloc: %x bytes, at:0x%x\n",
> x, result);
> write(1, buffer, strlen(buffer));
>
> VALGRIND_GET_ORIG_FN(fn);
> CALL_FN_W_W(result, fn, x);
>
> ////////////block 2
> (void) sprintf(buffer, "---------- 222 wrap malloc: %x bytes, at:0x%
> x\n", x, result);
> (void) write(1, buffer, strlen(buffer));
> catch_after += 3;
> result += 1;
>
> return result ;
> }
>
> #### in prog.c
> #include <stdio.h>
> #include <stdlib.h>
> extern int catch_before;
> extern int catch_after;
> int test2()
> { int * addr1 = 0;
> int * addr2 = 0;
>
> printf("1.1 catch:%d %d\n", catch_before, catch_after);
> addr1 = (int *)malloc( 10 * sizeof(int));
> printf("2.1 catch:%d %d addr:0x%x \n", catch_before, catch_after,
> addr1);
>
> printf("3.1 catch:%d %d\n", catch_before, catch_after);
> addr2 = (int *)malloc( 10 * sizeof(int));
> printf("4.1 catch:%d %d addr:0x%x \n", catch_before, catch_after,
> addr2);
> return (int)addr1 * (int)addr2;
> }
> int main()
> {
> return test2();
> }
>
> ########compile
> gcc -c -m32 prog.c -O0 -o prog.32.o
> gcc -o wrap.32.o -O0 -c -m32 test.wrap.sub.c
> gcc prog.32.o wrap.32.o -O0 -m32 -o prog.wrap.32.exe
> #########run with --tool=lackey or --tool=callgrind (eg. #valgrind
> --tool=lackey --trace-redir=yes ./prog.wrap.32.exe)
> 1.1 catch:0 0
> --12339-- REDIR: 0x40a7f50 (libc.so.6:malloc) redirected to 0x80487ce
> (malloc)
> ----------- 111 wrap malloc: 190 bytes, at:0x0
> ----------- 111 wrap malloc: 190 bytes, at:0x0
> ---------- 222 wrap malloc: 190 bytes, at:0x804b008
> ---------- 222 wrap malloc: 190 bytes, at:0x804b009
> 2.1 catch:2 6 addr:0x804b00a
> 3.1 catch:2 6
> ----------- 111 wrap malloc: 28 bytes, at:0x0
> ---------- 222 wrap malloc: 28 bytes, at:0x804b1a0
> 4.1 catch:3 9 addr:0x804b1a1
>
>
>
> shu...@hu...
>
> Best Regards.
>
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website, sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for all
> things parallel software development, from weekly thought leadership blogs to
> news, videos, case studies, tutorials and more. Take a look and join the
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________ Valgrind-developers mailing list Val...@li... https://lists.sourceforge.net/lists/listinfo/valgrind-developers |