| 178 | |
| 179 | == valgrid == |
| 180 | [http://valgrind.org/] |
| 181 | |
| 182 | ''Valgrind'' tools can detect many memory management and threading bugs, and profile your programs in detail. |
| 183 | |
| 184 | |
| 185 | === Detecting Invalid Access === |
| 186 | |
| 187 | {{{#!c |
| 188 | #include <stdio.h> |
| 189 | #include <stdlib.h> |
| 190 | #include <string.h> |
| 191 | |
| 192 | char * foo() { |
| 193 | char a[200]; |
| 194 | strcpy(a, "hello world cup\n"); |
| 195 | return a; |
| 196 | } |
| 197 | |
| 198 | int main() { |
| 199 | char * a = foo(); |
| 200 | char c = a[0]; |
| 201 | printf("a[0] = %c\n", c); |
| 202 | printf("a = %s\n", a); |
| 203 | return 0; |
| 204 | } |
| 205 | }}} |
| 206 | |
| 207 | Start an interactive session, |
| 208 | {{{#!bash |
| 209 | [fuji@cypress2 ~]$ idev -c 1 --gres=mic:0 |
| 210 | Requesting 1 node(s) task(s) to workshop queue of workshop partition |
| 211 | 1 task(s)/node, 1 cpu(s)/task, mic:0 MIC device(s)/node |
| 212 | Time: 0 (hr) 60 (min). |
| 213 | Submitted batch job 52605 |
| 214 | JOBID=52605 begin on cypress01-089 |
| 215 | --> Creating interactive terminal session (login) on node cypress01-089. |
| 216 | --> You have 0 (hr) 60 (min). |
| 217 | Last login: Wed Aug 19 21:05:45 2015 from cypress2.cm.cluster |
| 218 | [fuji@cypress01-089 ~]$ |
| 219 | }}} |
| 220 | compile and run, |
| 221 | {{{#!bash |
| 222 | [fuji@cypress01-089 ~]$ module load intel-psxe/2015-update1 |
| 223 | [fuji@cypress01-089 ~]$ icc off_stack.c |
| 224 | off_stack.c(8): warning #1251: returning pointer to local variable |
| 225 | return a; |
| 226 | ^ |
| 227 | |
| 228 | [fuji@cypress01-089 ~]$ ./a.out |
| 229 | a[0] = h |
| 230 | a = hello world cup |
| 231 | }}} |
| 232 | |
| 233 | {{{#!bash |
| 234 | [fuji@cypress01-089 ~]$ icc -O0 -g off_stack.c |
| 235 | off_stack.c(8): warning #1251: returning pointer to local variable |
| 236 | return a; |
| 237 | ^ |
| 238 | |
| 239 | [fuji@cypress01-089 ~]$ valgrind ./a.out |
| 240 | ==33367== Memcheck, a memory error detector |
| 241 | ==33367== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. |
| 242 | ==33367== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info |
| 243 | ==33367== Command: ./a.out |
| 244 | ==33367== |
| 245 | ==33367== Invalid read of size 1 |
| 246 | ==33367== at 0x4005C5: main (off_stack.c:13) |
| 247 | ==33367== Address 0x7feffdd50 is just below the stack ptr. To suppress, use: --workaround-gcc296-bugs=yes |
| 248 | ==33367== |
| 249 | a[0] = h |
| 250 | a = |
| 251 | ==33367== |
| 252 | ==33367== HEAP SUMMARY: |
| 253 | ==33367== in use at exit: 0 bytes in 0 blocks |
| 254 | ==33367== total heap usage: 0 allocs, 0 frees, 0 bytes allocated |
| 255 | ==33367== |
| 256 | ==33367== All heap blocks were freed -- no leaks are possible |
| 257 | ==33367== |
| 258 | ==33367== For counts of detected and suppressed errors, rerun with: -v |
| 259 | ==33367== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6) |
| 260 | [fuji@cypress01-089 ~]$ |
| 261 | }}} |
| 262 | |
| 263 | |