Download presentation
Presentation is loading. Please wait.
Published byDinah Cobb Modified over 9 years ago
1
Об отладке программ МО ВВС ИВМ и МГ СО РАН Городничев Максим Александрович
2
Отладочный вывод Буферизованный вывод printf(“Программа пришла сюда\n”); std::cout << “Текст\n” Небуферизованный вывод fprintf(stderr, “Программа пришла сюда\n”); std::cerr << “Текст\n”;
3
Отладочный вывод Очистка буфера: Cprintf(“Text\n”); fflush(stdout); C++std::cout << “Text\n”; std::cout.flush();
4
Отладка в gdb. Пример ошибочной программы. #include int f() { int* p = 0; *p = 100; return *p; } void g() { printf("%d", f()); } int main() { g(); } [maxim@ssd17 tests]$./a.out Segmentation fault
5
Отладка в gdb. Компиляция. [maxim@ssd17 tests]$ gcc –g3 segfault.c
6
Отладка в gdb. Запуск программы в отладчике. [maxim@ssd17 tests]$ gdb./a.out GNU gdb 6.6 Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-suse-linux"... Using host libthread_db library "/lib/libthread_db.so.1". (gdb) run
7
Отладка программы в gdb. Анализ ошибки Starting program: /home/maxim/work/tests/a.out Program received signal SIGSEGV, Segmentation fault. 0x08048364 in f () at segfault.c:5 5 *p = 100; (gdb) backtrace #0 0x08048364 in f () at segfault.c:5 #1 0x0804837c in g () at segfault.c:11 #2 0x080483a4 in main () at segfault.c:16 (gdb) print p $1 = (int *) 0x0 (gdb) quit The program is running. Exit anyway? (y or n) y
8
Отладка в gdb. Пошаговое выполнение. (gdb) break g Breakpoint 1 at 0x8048377: file segfault.c, line 11. (gdb) run Starting program: /home/maxim/work/tests/a.out warning:.dynamic section for "/lib/libc.so.6" is not at the expected address warning: difference appears to be caused by prelink, adjusting expectations Breakpoint 1, g () at segfault.c:11 11 printf("%d", f()); (gdb) next Program received signal SIGSEGV, Segmentation fault. 0x08048364 in f () at segfault.c:5 5 *p = 100;
9
Отладка в gdb. Пошаговое выполнение. (gdb) step f () at segfault.c:4 4 int* p = 0; (gdb) list 1 #include 2 int f() 3 { 4 int* p = 0; 5 *p = 100; 6 return *p; 7 } 8 9 void g() 10{ (gdb) continue
10
Вопросы?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.