- 论坛徽章:
- 0
|
简单修改了一下。
- /* prompting shell version 1
- * * Prompts for the command and its arguments.
- * * Builds the argument vector for the call to execvp.
- * * Uses execvp(), and never returns.
- * */
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXARGS 2 /* cmdline args */
- #define ARGLEN 100 /* token length */
- int execute( char *arglist[] );
- int main()
- {
- char *arglist[MAXARGS+1]; /* an array of ptrs */
- int numargs; /* index into array */
- char argbuf[ARGLEN]; /* read stuff here */
- char *makestring(); /* malloc etc */
- numargs = 0;
- while ( numargs < MAXARGS )
- {
- printf("Arg[%d]? ", numargs);
- if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n' )
- arglist[numargs++] = makestring(argbuf);
- else
- {
- if ( numargs > 0 ){ /* any args? */
- arglist[numargs]=NULL; /* close list */
- execute( arglist ); /* do it */
- numargs = 0; /* and reset */
- }
- }
- }
- return 0;
- }
- int execute( char *arglist[] )
- /*
- * * use execvp to do it
- * */
- {
- execvp(arglist[0], arglist); /* do it */
- perror("execvp failed");
- exit(1);
- }
- char * makestring( char *buf )
- /*
- * * trim off newline and create storage for the string
- * */
- {
- char *cp;
- buf[strlen(buf)-1] = '\0'; /* trim newline */
- cp = malloc( strlen(buf)+1 ); /* get memory */
- if ( cp == NULL ){ /* or die */
- fprintf(stderr,"no memory\n");
- exit(1);
- }
- strcpy(cp, buf); /* copy chars */
- return cp; /* return ptr */
- }
复制代码
- [jimmy@local memory]$ valgrind --tool=memcheck -v --leak-check=yes ./test_shell
- ==7138== Memcheck, a memory error detector for x86-linux.
- ==7138== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
- ==7138== Using valgrind-2.2.0, a program supervision framework for x86-linux.
- ==7138== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
- ==7138== Valgrind library directory: /usr/lib/valgrind
- ==7138== Command line
- ==7138== ./test_shell
- ==7138== Startup, with flags:
- ==7138== --tool=memcheck
- ==7138== -v
- ==7138== --leak-check=yes
- ==7138== Contents of /proc/version:
- ==7138== Linux version 2.6.9-22.EL ([email]bhcompile@porky.build.redhat.com[/email]) (gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)) #1 Mon Sep 19 18:20:28 EDT 2005
- ==7138== Reading syms from /home/jimmy/learn/memory/test_shell (0x8048000)
- ==7138== object doesn't have any debug info
- ==7138== Reading syms from /lib/ld-2.3.4.so (0x1B8E4000)
- ==7138== object doesn't have any debug info
- ==7138== Reading syms from /usr/lib/valgrind/stage2 (0xB0000000)
- ==7138== Reading syms from /lib/ld-2.3.4.so (0xB1000000)
- ==7138== object doesn't have any debug info
- ==7138== Reading syms from /usr/lib/valgrind/vgskin_memcheck.so (0xB7C96000)
- ==7138== Reading syms from /lib/tls/libc-2.3.4.so (0xB7EBF000)
- ==7138== object doesn't have any debug info
- ==7138== Reading syms from /lib/libdl-2.3.4.so (0xB7FEA000)
- ==7138== object doesn't have any debug info
- ==7138== Reading suppressions file: /usr/lib/valgrind/default.supp
- ==7138== REDIRECT soname:libc.so.6(__GI___errno_location) to soname:libpthread.so.0(__errno_location)
- ==7138== REDIRECT soname:libc.so.6(__errno_location) to soname:libpthread.so.0(__errno_location)
- ==7138== REDIRECT soname:libc.so.6(__GI___h_errno_location) to soname:libpthread.so.0(__h_errno_location)
- ==7138== REDIRECT soname:libc.so.6(__h_errno_location) to soname:libpthread.so.0(__h_errno_location)
- ==7138== REDIRECT soname:libc.so.6(__GI___res_state) to soname:libpthread.so.0(__res_state)
- ==7138== REDIRECT soname:libc.so.6(__res_state) to soname:libpthread.so.0(__res_state)
- ==7138== REDIRECT soname:libc.so.6(stpcpy) to *vgpreload_memcheck.so*(stpcpy)
- ==7138== REDIRECT soname:libc.so.6(strnlen) to *vgpreload_memcheck.so*(strnlen)
- ==7138== REDIRECT soname:ld-linux.so.2(stpcpy) to *vgpreload_memcheck.so*(stpcpy)
- ==7138== REDIRECT soname:ld-linux.so.2(strchr) to *vgpreload_memcheck.so*(strchr)
- ==7138==
- ==7138== Reading syms from /usr/lib/valgrind/vg_inject.so (0x1B8FC000)
- ==7138== Reading syms from /usr/lib/valgrind/vgpreload_memcheck.so (0x1B901000)
- ==7138== TRANSLATE: 0x1B8F5F00 redirected to 0x1B904100
- ==7138== Reading syms from /lib/tls/libc-2.3.4.so (0x875000)
- ==7138== object doesn't have any debug info
- ==7138== TRANSLATE: 0x1B8E47A0 redirected to 0x52BFF040
- Arg[0]? hello
- ==7138== TRANSLATE: 0x8D8F00 redirected to 0x1B9048F8
- Arg[1]? hello
- ==7138== TRANSLATE: 0x8D6E30 redirected to 0x1B904E24
- ==7138==
- ==7138== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 1)
- --7138--
- --7138-- supp: 12 dl_relocate_object/dl_main
- ==7138== malloc/free: in use at exit: 12 bytes in 2 blocks.
- ==7138== malloc/free: 2 allocs, 0 frees, 12 bytes allocated.
- ==7138==
- ==7138== searching for pointers to 2 not-freed blocks.
- ==7138== checked 1373240 bytes.
- ==7138==
- ==7138== [color=Red]12 bytes in 2 blocks are definitely lost in loss record 1 of 1[/color]
- ==7138== at 0x1B904984: malloc (vg_replace_malloc.c:131)
- ==7138== by 0x8048675: makestring (in /home/jimmy/learn/memory/test_shell)
- ==7138== by 0x80485D3: main (in /home/jimmy/learn/memory/test_shell)
- ==7138==
- ==7138== LEAK SUMMARY:
- ==7138== definitely lost: 12 bytes in 2 blocks.
- ==7138== possibly lost: 0 bytes in 0 blocks.
- ==7138== still reachable: 0 bytes in 0 blocks.
- ==7138== suppressed: 0 bytes in 0 blocks.
- ==7138== Reachable blocks (those to which a pointer was found) are not shown.
- ==7138== To see them, rerun with: --show-reachable=yes
- --7138-- TT/TC: 0 tc sectors discarded.
- --7138-- 2209 tt_fast misses.
- --7138-- translate: new 2067 (36681 -> 485142; ratio 132:10)
- --7138-- discard 2 (59 -> 840; ratio 142:10).
- --7138-- chainings: 1306 chainings, 3 unchainings.
- --7138-- dispatch: 0 jumps (bb entries); of them 4627 (462700%) unchained.
- --7138-- 24/2274 major/minor sched events.
- --7138-- reg-alloc: 474 t-req-spill, 90449+3948 orig+spill uis,
- --7138-- 11433 total-reg-rank
- --7138-- sanity: 22 cheap, 1 expensive checks.
- --7138-- ccalls: 7810 C calls, 54% saves+restores avoided (25038 bytes)
- --7138-- 10348 args, avg 0.87 setup instrs each (2658 bytes)
- --7138-- 0% clear the stack (23325 bytes)
- --7138-- 3535 retvals, 30% of reg-reg movs avoided (2056 bytes)
复制代码
[ 本帖最后由 futuregod 于 2008-12-9 10:20 编辑 ] |
|