Saturday, December 1, 2012

SEMBOL TABLOSU

C ya da C++'de geliştirdiğimiz uygulamalarda hata ayıklama yapmamız gerektiğinde, kodu GNU c derleyeyicisi ile -g seçeneği ile derlememiz gerekir. Aksi halde gdb ile uygulamada hata ayıklamak istediğimizde sembol tablosunu bulamadığına ilişkin uyarı verecektir:
[guru@godel ~]$ g++ -o lottery lottery.cpp -std=c++11
[guru@godel ~]$ gdb lottery 
GNU gdb (GDB) Fedora (7.4.50.20120120-42.fc17)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/guru/lottery...(no debugging symbols found)...done.
(gdb) quit
[guru@godel ~]$ g++ -g -o lottery lottery.cpp -std=c++11
[guru@godel ~]$ gdb lottery 
GNU gdb (GDB) Fedora (7.4.50.20120120-42.fc17)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/guru/lottery...done.

lottery.cpp: Sayısal loto için 6 adet [1,49] aralığında birbirinden farklı sıralı sayı üretir

#include <iostream>
#include <algorithm>
#include <random>
#include <list>

using namespace std;

int main(){
   random_device rd; 
   mt19937 gen(rd());
   uniform_int_distribution<uint32_t> dist(1,49);
   list<uint32_t> lst;
   while ( lst.size() < 6 ){
      uint32_t r;
      do {
        r= dist(gen);
      } while(find(lst.begin(),lst.end(),r)!=lst.end());
      lst.push_front(r);
   }   
   lst.sort();
   for (auto x : lst)
       cout <<  x << endl;
   return 0;
}

-g seçeneği ile derlendiğinde çalıştırılabilir dosyanın içinde sembol tablosu da yer alacaktır. Hata ayıklayıcı sembol tablosunu, kod ile bellek alanlarını eşleştirmek için kullanmaktadır. Ancak bunun bir yan etkisi bulunmaktadır. Sembol tablosu çalıştırılabilir dosyanın içinde yer aldığı için dosyanın boyu büyümektedir. İstenirse sembol tablosunu çalıştırılabilir dosyanın dışına ayrı bir dosyaya alınabilir. Bunun için strip ve objcopy komutlarından yararlanıyoruz.
[guru@godel ~]$ objcopy --only-keep-debug lottery lottery.debug
[guru@godel ~]$ strip --strip-debug --strip-unneeded lottery
[guru@godel ~]$ objcopy --add-gnu-debuglink=lottery.debug lottery
[guru@godel ~]$ ls -lh lottery*
-rwxrwxr-x. 1 student student 17K Dec  1 16:05 lottery
-rw-rw-r--. 1 student student 506 Dec  1 15:50 lottery.cpp
-rwxrwxr-x. 1 student student 77K Dec  1 16:04 lottery.debug
[guru@godel ~]$ gdb lottery
GNU gdb (GDB) Fedora (7.4.50.20120120-42.fc17)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/guru/lottery...Reading symbols from /home/guru/lottery.debug...done.
(gdb) list 1,10
1 #include <iostream>
2 #include <algorithm>
3 #include <random>
4 #include <list>
5
6 using namespace std;
7
8 int main(){
9   random_device rd;
10   mt19937 gen(rd());
(gdb) list 11,20
11   uniform_int_distribution<uint32_t> dist(1,49);
12   list<uint32_t> lst;
13   while ( lst.size() < 6 ){
14      uint32_t r;
15      do {
16        r= dist(gen);
17      } while(find(lst.begin(),lst.end(),r)!=lst.end()) ;
18      numbers.push_front(r);
19   }
20   lst.sort();
(gdb) break 13
Breakpoint 1 at 0x401019: file lottery.cpp, line 13.
(gdb) break 20
Breakpoint 2 at 0x4010d2: file lottery.cpp, line 20.
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401019 in main() at lottery.cpp:13
2       breakpoint     keep y   0x00000000004010d2 in main() at lottery.cpp:20
(gdb) run
Starting program: /home/guru/lottery 

Breakpoint 1, main () at lottery.cpp:13
13   while ( lst.size() < 6 ){
(gdb) print lst
$1 = empty std::list
(gdb) cont
Continuing.

Breakpoint 2, main () at lottery.cpp:20
20   lst.sort();
(gdb) print lst
$2 = std::list = {[0] = 23, [1] = 42, [2] = 7, [3] = 22, [4] = 21, [5] = 36}
(gdb) cont
Continuing.
7
21
22
23
36
42
[Inferior 1 (process 2463) exited normally]
(gdb) quit


No comments:

Post a Comment