Ik probeer een C programma te debuggen, wat compiled, echter ik krijg een segmentation fault zodra het programma uitgevoerd wordt. Als ik de asprint regel uitzet door er commentaar van te maken geeft het programma geen fout bij uitvoerne. Op internet gezocht hoe dit opgelost kan worden, geen oplossing gevonden. Wel een hint naar Valgrind. Ik compile onder linux met: gcc segfault.c -g
valgrind --leak-check=full -v ./a.out
geeft:
==2646== Memcheck, a memory error detector
==2646== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2646== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==2646== Command: ./a.out
==2646==
--2646-- Valgrind options:
--2646-- --leak-check=full
--2646-- -v
--2646-- Contents of /proc/version:
--2646-- Linux version 3.16.7-21-desktop (geeko@buildhost) (gcc version 4.8.3 20140627 [gcc-4_8-branch revision 212064] (SUSE Linux) ) #1 SMP PREEMPT Tue Apr 14 07:11:37 UTC 2015 (93c1539)
--2646-- Arch and hwcaps: AMD64, LittleEndian, amd64-cx16-lzcnt-rdtscp-sse3-avx-bmi
--2646-- Page sizes: currently 4096, max supported 4096
--2646-- Valgrind library directory: /usr/lib64/valgrind
--2646-- Reading syms from /home/roel/a.out
--2646-- Reading syms from /lib64/ld-2.19.so
--2646-- Reading syms from /usr/lib64/valgrind/memcheck-amd64-linux
--2646-- object doesn't have a symbol table
--2646-- object doesn't have a dynamic symbol table
--2646-- Scheduler: using generic scheduler lock implementation.
--2646-- Reading suppressions file: /usr/lib64/valgrind/default.supp
==2646== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-2646-by-roel-on-WHYN-001
==2646== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-2646-by-roel-on-WHYN-001
==2646== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-2646-by-roel-on-WHYN-001
==2646==
==2646== TO CONTROL THIS PROCESS USING vgdb (which you probably
==2646== don't want to do, unless you know exactly what you're doing,
==2646== or are doing some strange experiment):
==2646== /usr/lib64/valgrind/../../bin/vgdb --pid=2646 ...command...
==2646==
==2646== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==2646== /path/to/gdb ./a.out
==2646== and then give GDB the following command
==2646== target remote | /usr/lib64/valgrind/../../bin/vgdb --pid=2646
==2646== --pid is optional if only one valgrind process is running
==2646==
--2646-- REDIR: 0x4017810 (ld-linux-x86-64.so.2:strlen) redirected to 0x3806bd01 (???)
--2646-- Reading syms from /usr/lib64/valgrind/vgpreload_core-amd64-linux.so
--2646-- object doesn't have a symbol table
--2646-- Reading syms from /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
--2646-- object doesn't have a symbol table
==2646== WARNING: new redirection conflicts with existing -- ignoring it
--2646-- old: 0x04017810 (strlen ) R-> (0000.0) 0x3806bd01 ???
--2646-- new: 0x04017810 (strlen ) R-> (2007.0) 0x04c2c730 strlen
--2646-- REDIR: 0x40175c0 (ld-linux-x86-64.so.2:index) redirected to 0x4c2c2e0 (index)
--2646-- REDIR: 0x40177e0 (ld-linux-x86-64.so.2:strcmp) redirected to 0x4c2d880 (strcmp)
--2646-- REDIR: 0x4018510 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x4c30330 (mempcpy)
--2646-- Reading syms from /lib64/libc-2.19.so
--2646-- REDIR: 0x4eba530 (libc.so.6:strcasecmp) redirected to 0x4a23770 (_vgnU_ifunc_wrapper)
--2646-- REDIR: 0x4ebc820 (libc.so.6:strncasecmp) redirected to 0x4a23770 (_vgnU_ifunc_wrapper)
--2646-- REDIR: 0x4eb9cb0 (libc.so.6:memcpy@GLIBC_2.2.5) redirected to 0x4a23770 (_vgnU_ifunc_wrapper)
--2646-- REDIR: 0x4eb8030 (libc.so.6:rindex) redirected to 0x4c2bfc0 (rindex)
--2646-- REDIR: 0x4eb06e0 (libc.so.6:malloc) redirected to 0x4c290a0 (malloc)
--2646-- REDIR: 0x4ec0ec0 (libc.so.6:strchrnul) redirected to 0x4c2ff40 (strchrnul)
==2646== Invalid read of size 1
==2646== at 0x4E7CF80: vfprintf (in /lib64/libc-2.19.so)
==2646== by 0x4EA67C2: vasprintf (in /lib64/libc-2.19.so)
==2646== by 0x4E85806: asprintf (in /lib64/libc-2.19.so)
==2646== by 0x400623: main (in /home/roel/a.out)
==2646== Address 0x61 is not stack'd, malloc'd or (recently) free'd
==2646==
==2646==
==2646== Process terminating with default action of signal 11 (SIGSEGV)
==2646== Access not within mapped region at address 0x61
==2646== at 0x4E7CF80: vfprintf (in /lib64/libc-2.19.so)
==2646== by 0x4EA67C2: vasprintf (in /lib64/libc-2.19.so)
==2646== by 0x4E85806: asprintf (in /lib64/libc-2.19.so)
==2646== by 0x400623: main (in /home/roel/a.out)
==2646== If you believe this happened as a result of a stack
==2646== overflow in your program's main thread (unlikely but
==2646== possible), you can try to increase the size of the
==2646== main thread stack using the --main-stacksize= flag.
==2646== The main thread stack size used in this run was 8388608.
--2646-- REDIR: 0x4eb0d20 (libc.so.6:free) redirected to 0x4c2a310 (free)
==2646==
==2646== HEAP SUMMARY:
==2646== in use at exit: 100 bytes in 1 blocks
==2646== total heap usage: 1 allocs, 0 frees, 100 bytes allocated
==2646==
==2646== Searching for pointers to 1 not-freed blocks
==2646== Checked 66,720 bytes
==2646==
==2646== LEAK SUMMARY:
==2646== definitely lost: 0 bytes in 0 blocks
==2646== indirectly lost: 0 bytes in 0 blocks
==2646== possibly lost: 0 bytes in 0 blocks
==2646== still reachable: 100 bytes in 1 blocks
==2646== suppressed: 0 bytes in 0 blocks
==2646== Reachable blocks (those to which a pointer was found) are not shown.
==2646== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2646==
==2646== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==2646==
==2646== 1 errors in context 1 of 1:
==2646== Invalid read of size 1
==2646== at 0x4E7CF80: vfprintf (in /lib64/libc-2.19.so)
==2646== by 0x4EA67C2: vasprintf (in /lib64/libc-2.19.so)
==2646== by 0x4E85806: asprintf (in /lib64/libc-2.19.so)
==2646== by 0x400623: main (in /home/roel/a.out)
==2646== Address 0x61 is not stack'd, malloc'd or (recently) free'd
==2646==
==2646== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault
Hoe kan ik dit oplossen?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| #include <stdio.h> #include <stdlib.h> #include <string.h> //#include <curl/curl.h> int main(void) { char aa=0;char ab=0; const char validurlchar[38]= "abcdefghijklmnopqrstuvwxyz0123456789-_"; char* source; asprintf(&source,"%s%s%.nl",validurlchar[aa],validurlchar[ab]); free(source); } |
valgrind --leak-check=full -v ./a.out
geeft:
==2646== Memcheck, a memory error detector
==2646== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2646== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==2646== Command: ./a.out
==2646==
--2646-- Valgrind options:
--2646-- --leak-check=full
--2646-- -v
--2646-- Contents of /proc/version:
--2646-- Linux version 3.16.7-21-desktop (geeko@buildhost) (gcc version 4.8.3 20140627 [gcc-4_8-branch revision 212064] (SUSE Linux) ) #1 SMP PREEMPT Tue Apr 14 07:11:37 UTC 2015 (93c1539)
--2646-- Arch and hwcaps: AMD64, LittleEndian, amd64-cx16-lzcnt-rdtscp-sse3-avx-bmi
--2646-- Page sizes: currently 4096, max supported 4096
--2646-- Valgrind library directory: /usr/lib64/valgrind
--2646-- Reading syms from /home/roel/a.out
--2646-- Reading syms from /lib64/ld-2.19.so
--2646-- Reading syms from /usr/lib64/valgrind/memcheck-amd64-linux
--2646-- object doesn't have a symbol table
--2646-- object doesn't have a dynamic symbol table
--2646-- Scheduler: using generic scheduler lock implementation.
--2646-- Reading suppressions file: /usr/lib64/valgrind/default.supp
==2646== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-2646-by-roel-on-WHYN-001
==2646== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-2646-by-roel-on-WHYN-001
==2646== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-2646-by-roel-on-WHYN-001
==2646==
==2646== TO CONTROL THIS PROCESS USING vgdb (which you probably
==2646== don't want to do, unless you know exactly what you're doing,
==2646== or are doing some strange experiment):
==2646== /usr/lib64/valgrind/../../bin/vgdb --pid=2646 ...command...
==2646==
==2646== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==2646== /path/to/gdb ./a.out
==2646== and then give GDB the following command
==2646== target remote | /usr/lib64/valgrind/../../bin/vgdb --pid=2646
==2646== --pid is optional if only one valgrind process is running
==2646==
--2646-- REDIR: 0x4017810 (ld-linux-x86-64.so.2:strlen) redirected to 0x3806bd01 (???)
--2646-- Reading syms from /usr/lib64/valgrind/vgpreload_core-amd64-linux.so
--2646-- object doesn't have a symbol table
--2646-- Reading syms from /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
--2646-- object doesn't have a symbol table
==2646== WARNING: new redirection conflicts with existing -- ignoring it
--2646-- old: 0x04017810 (strlen ) R-> (0000.0) 0x3806bd01 ???
--2646-- new: 0x04017810 (strlen ) R-> (2007.0) 0x04c2c730 strlen
--2646-- REDIR: 0x40175c0 (ld-linux-x86-64.so.2:index) redirected to 0x4c2c2e0 (index)
--2646-- REDIR: 0x40177e0 (ld-linux-x86-64.so.2:strcmp) redirected to 0x4c2d880 (strcmp)
--2646-- REDIR: 0x4018510 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x4c30330 (mempcpy)
--2646-- Reading syms from /lib64/libc-2.19.so
--2646-- REDIR: 0x4eba530 (libc.so.6:strcasecmp) redirected to 0x4a23770 (_vgnU_ifunc_wrapper)
--2646-- REDIR: 0x4ebc820 (libc.so.6:strncasecmp) redirected to 0x4a23770 (_vgnU_ifunc_wrapper)
--2646-- REDIR: 0x4eb9cb0 (libc.so.6:memcpy@GLIBC_2.2.5) redirected to 0x4a23770 (_vgnU_ifunc_wrapper)
--2646-- REDIR: 0x4eb8030 (libc.so.6:rindex) redirected to 0x4c2bfc0 (rindex)
--2646-- REDIR: 0x4eb06e0 (libc.so.6:malloc) redirected to 0x4c290a0 (malloc)
--2646-- REDIR: 0x4ec0ec0 (libc.so.6:strchrnul) redirected to 0x4c2ff40 (strchrnul)
==2646== Invalid read of size 1
==2646== at 0x4E7CF80: vfprintf (in /lib64/libc-2.19.so)
==2646== by 0x4EA67C2: vasprintf (in /lib64/libc-2.19.so)
==2646== by 0x4E85806: asprintf (in /lib64/libc-2.19.so)
==2646== by 0x400623: main (in /home/roel/a.out)
==2646== Address 0x61 is not stack'd, malloc'd or (recently) free'd
==2646==
==2646==
==2646== Process terminating with default action of signal 11 (SIGSEGV)
==2646== Access not within mapped region at address 0x61
==2646== at 0x4E7CF80: vfprintf (in /lib64/libc-2.19.so)
==2646== by 0x4EA67C2: vasprintf (in /lib64/libc-2.19.so)
==2646== by 0x4E85806: asprintf (in /lib64/libc-2.19.so)
==2646== by 0x400623: main (in /home/roel/a.out)
==2646== If you believe this happened as a result of a stack
==2646== overflow in your program's main thread (unlikely but
==2646== possible), you can try to increase the size of the
==2646== main thread stack using the --main-stacksize= flag.
==2646== The main thread stack size used in this run was 8388608.
--2646-- REDIR: 0x4eb0d20 (libc.so.6:free) redirected to 0x4c2a310 (free)
==2646==
==2646== HEAP SUMMARY:
==2646== in use at exit: 100 bytes in 1 blocks
==2646== total heap usage: 1 allocs, 0 frees, 100 bytes allocated
==2646==
==2646== Searching for pointers to 1 not-freed blocks
==2646== Checked 66,720 bytes
==2646==
==2646== LEAK SUMMARY:
==2646== definitely lost: 0 bytes in 0 blocks
==2646== indirectly lost: 0 bytes in 0 blocks
==2646== possibly lost: 0 bytes in 0 blocks
==2646== still reachable: 100 bytes in 1 blocks
==2646== suppressed: 0 bytes in 0 blocks
==2646== Reachable blocks (those to which a pointer was found) are not shown.
==2646== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2646==
==2646== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==2646==
==2646== 1 errors in context 1 of 1:
==2646== Invalid read of size 1
==2646== at 0x4E7CF80: vfprintf (in /lib64/libc-2.19.so)
==2646== by 0x4EA67C2: vasprintf (in /lib64/libc-2.19.so)
==2646== by 0x4E85806: asprintf (in /lib64/libc-2.19.so)
==2646== by 0x400623: main (in /home/roel/a.out)
==2646== Address 0x61 is not stack'd, malloc'd or (recently) free'd
==2646==
==2646== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault
Hoe kan ik dit oplossen?