[[PageOutline]] = Code Debugging = Ideally, code should be debuged on your desktop computer before being moved to a cluster environment. There are a number of debugging techniques, which you can learn from the internet. == print == Insert 'print' into the source code. in C/C++ {{{#!c /* check */ #ifdef DEBUG if (info == 0) printf("successfully done\n"); #endif }}} in Fortran {{{#!fortran #ifdef debug if (info == 0) then print *,"successfully done" endif #endif }}} Compile with ''-DDEBUG'' option {{{ icc -g -pg -DDEBUG -c stokeslet2d.c }}} Makefile {{{#!make # # CCS WORKSHOP # Stokes Flow in a Cavity # # Makefile # # TARGET = ex32s ex32m # ALL: $(TARGET) # CC = icc #CFLAGS = -O3 CFLAGS = -g -pg -DDEBUG # # # SRC_EX32c = ex32.c stokeslet2d.c gmres.c # # MKL_SQ_LIBS = -L$(MKLROOT)/lib/intel64/ \ -I$(MKLROOT)/mkl/include \ -Wl,--start-group \ $(MKLROOT)/lib/intel64/libmkl_intel_lp64.a \ $(MKLROOT)/lib/intel64/libmkl_sequential.a \ $(MKLROOT)/lib/intel64/libmkl_core.a \ -Wl,--end-group \ -lpthread # MKL_MT_LIBS = -L$(MKLROOT)/lib/intel64/ \ -I$(MKLROOT)/mkl/include \ -Wl,--start-group \ $(MKLROOT)/lib/intel64/libmkl_intel_lp64.a \ $(MKLROOT)/lib/intel64/libmkl_intel_thread.a \ $(MKLROOT)/lib/intel64/libmkl_core.a \ -Wl,--end-group \ -liomp5 \ -lpthread # # # OBJ_EX32c = $(SRC_EX32c:.c=.o) # # ex32s : $(OBJ_EX32c) $(CC) $(CFLAGS) -o $@ $(OBJ_EX32c) $(MKL_SQ_LIBS) ex32m : $(OBJ_EX32c) $(CC) $(CFLAGS) -o $@ $(OBJ_EX32c) $(MKL_MT_LIBS) # # %.o : %.c $(CC) $(CFLAGS) -c $< # clean: rm -f *.o $(TARGET) }}} == GDB == [http://www.gnu.org/software/gdb/] To debug with '''GDB''', submit an interactive job. [[https://wiki.hpc.tulane.edu/trac/wiki/cypress/using#SubmittingInteractiveJobs|See here]] Compiling with '''-g''' option {{{ icc -g -pg -DDEBUG -c stokeslet2d.c }}} run '''gdb''' {{{ user@host>gdb ./ex32s GNU gdb (GDB) Red Hat Enterprise Linux (7.2-56.el6) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: ... Reading symbols from /ccs-autofs/u01/fuji/LabWork/FlowInCavity/ex32s...done. (gdb) }}} show source by command, list '''line#''' {{{ (gdb) list 44 39 printf("Usage:%s [Depth of Cavity]\n",argv[0]); 40 exit(-1); 41 } 42 43 /* get inputed depth */ 44 dp = atof(argv[1]); 45 46 /* # of particles in depth */ 47 numpdepth = (int)(dp / EPSILON + 0.5); 48 (gdb) }}} set breakpoint by command, '''b line#''' {{{ (gdb) b 47 Breakpoint 1 at 0x4044c2: file ex32.c, line 47. (gdb) }}} '''run [command line option]''' {{{ (gdb) run 5 Starting program: /ccs-autofs/u01/fuji/LabWork/FlowInCavity/ex32s 1 [Thread debugging using libthread_db enabled] Breakpoint 1, main (argc=2, argv=0x7fffffffd5c8) at ex32.c:47 47 numpdepth = (int)(dp / EPSILON + 0.5); Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6_3.3.x86_64 (gdb) }}} print values {{{ (gdb) p dp $1 = 5 (gdb) p numpdepth $2 = 0 (gdb) }}} continue one step {{{ (gdb) next 50 numpwidth = (int)(1.0 / EPSILON + 0.5); (gdb) p numpdepth $3 = 1000 (gdb) }}} exit {{{ (gdb) quit A debugging session is active. Inferior 1 [process 6222] will be killed. Quit anyway? (y or n) y }}} == valgrid == [http://valgrind.org/] ''Valgrind'' tools can detect many memory management and threading bugs, and profile your programs in detail. === Detecting Invalid Access === {{{#!c #include #include #include char * foo() { char a[200]; strcpy(a, "hello world cup\n"); return a; } int main() { char * a = foo(); char c = a[0]; printf("a[0] = %c\n", c); printf("a = %s\n", a); return 0; } }}} Start an interactive session, {{{#!bash [fuji@cypress2 ~]$ idev -c 1 --gres=mic:0 Requesting 1 node(s) task(s) to workshop queue of workshop partition 1 task(s)/node, 1 cpu(s)/task, mic:0 MIC device(s)/node Time: 0 (hr) 60 (min). Submitted batch job 52605 JOBID=52605 begin on cypress01-089 --> Creating interactive terminal session (login) on node cypress01-089. --> You have 0 (hr) 60 (min). Last login: Wed Aug 19 21:05:45 2015 from cypress2.cm.cluster [fuji@cypress01-089 ~]$ }}} compile and run, {{{#!bash [fuji@cypress01-089 ~]$ module load intel-psxe/2015-update1 [fuji@cypress01-089 ~]$ icc off_stack.c off_stack.c(8): warning #1251: returning pointer to local variable return a; ^ [fuji@cypress01-089 ~]$ ./a.out a[0] = h a = hello world cup }}} {{{#!bash [fuji@cypress01-089 ~]$ icc -O0 -g off_stack.c off_stack.c(8): warning #1251: returning pointer to local variable return a; ^ [fuji@cypress01-089 ~]$ valgrind ./a.out ==33367== Memcheck, a memory error detector ==33367== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==33367== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==33367== Command: ./a.out ==33367== ==33367== Invalid read of size 1 ==33367== at 0x4005C5: main (off_stack.c:13) ==33367== Address 0x7feffdd50 is just below the stack ptr. To suppress, use: --workaround-gcc296-bugs=yes ==33367== a[0] = h a = ==33367== ==33367== HEAP SUMMARY: ==33367== in use at exit: 0 bytes in 0 blocks ==33367== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==33367== ==33367== All heap blocks were freed -- no leaks are possible ==33367== ==33367== For counts of detected and suppressed errors, rerun with: -v ==33367== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6) [fuji@cypress01-089 ~]$ }}}