|
From: Philippe W. <phi...@sk...> - 2015-04-01 19:31:55
|
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
|