2017-04-23 44 views
1

請參閱如何gdb無法打印全局變量dictionary在下面的代碼:爲什麼`gdb`看不到全局變量?

[email protected] ~ $ cat 148.cc 
#include <iostream> 
#include <string> 
#include <vector> 
#include <array> 
#include <utility> 
#include <algorithm> 
#include <sstream> 
using namespace std; 

using letters = array<int, 26>; 
letters emptylet = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
vector<pair<string, letters>> dictionary; 

void backtrace(string &orig, vector<string> &phrase, vector<string> &acc, letters &curr, letters &goal, decltype(dictionary)::iterator it) { 
    if(it == dictionary.end()) 
    return; 
    for(int i = 0; i < it->second.size(); i++) 
    curr[i] += it->second[i]; 
    acc.push_back(it->first); 
    bool all_same = true; 
    for(int i = 0; i < curr.size(); i++) 
    if(curr[i] > goal[i]) 
     goto clean; 
    else if(curr[i] != goal[i]) 
     all_same = false; 
    if(all_same) { 
    if(acc != phrase) { 
     cout << orig << " ="; 
     for(string word: acc) 
     cout << ' ' << word; 
     cout << '\n'; 
    } 
    } else 
    backtrace(orig, phrase, acc, curr, goal, it+1); 
clean: 
    acc.pop_back(); 
    for(int i = 0; i < it->second.size(); i++) 
    curr[i] -= it->second[i]; 
    backtrace(orig, phrase, acc, curr, goal, it+1); 
} 

int main() 
{ 
    while((cin>>ws).peek()!='#') { 
    string word; 
    cin >> word; 
    letters let = emptylet; 
    for(char c: word) 
     let[c-'A']++; 
    dictionary.push_back(make_pair(word, let)); 
    } 
    while((cin>>ws).peek()!='#') { 
    string s; 
    getline(cin, s); 
    string orig = s; 
    stringstream ss(s+'\n'); 
    vector<string> phrase; 
    while(cin>>s){ 
     phrase.push_back(s); 
    } 
    sort(phrase.begin(), phrase.end()); 
    letters let = emptylet; 
    for(string wrd: phrase) 
     for(char c: wrd) 
     let[c-'A']++; 
    vector<string> empt; 
    backtrace(orig, phrase, empt, emptylet, let, dictionary.begin()); 
    } 
} 
[email protected] ~ $ g++ -g -std=c++11 -o 148 148.cc 
[email protected] ~ $ cat 148.in 
ABC 
AND 
DEF 
DXZ 
K 
KX 
LJSRT 
LT 
PT 
PTYYWQ 
Y 
YWJSRQ 
ZD 
ZZXY 
# 
ZZXY ABC DEF 
SXZYTWQP KLJ YRTD 
ZZXY YWJSRQ PTYYWQ ZZXY 
# 
[email protected] ~ $ gdb ./148 
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1 
Copyright (C) 2016 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-linux-gnu". 
Type "show configuration" for configuration details. 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>. 
Find the GDB manual and other documentation resources online at: 
<http://www.gnu.org/software/gdb/documentation/>. 
For help, type "help". 
Type "apropos word" to search for commands related to "word"... 
Reading symbols from ./148...done. 
(gdb) break 52 
Breakpoint 1 at 0x401d0f: file 148.cc, line 52. 
(gdb) r < 148.in 
Starting program: /home/m/148 < 148.in 

Breakpoint 1, main() at 148.cc:52 
52 while((cin>>ws).peek()!='#') { 
(gdb) p dictionary 
No symbol "dictionary" in current context. 
(gdb) p di 
dictionary[abi:cxx11] dir_data     dirfd     dirstream.h    div      divmod_1.c 
difftime     dirent     dirfd.c     disallow_malloc_check div.c     divrem.c 
difftime.c    dirent.h     dirname     distinguish_extX   div_t      
digits_dots.c   dirent64     dirname.c    distinguish_extX.isra.0 divide     
(gdb) p dictionary[abi:cxx11] 
No symbol "dictionary" in current context. 
(gdb) quit 
A debugging session is active. 

    Inferior 1 [process 3815] will be killed. 

Quit anyway? (y or n) y 
[email protected] ~ $ g++ --version 
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 
Copyright (C) 2015 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

[email protected] ~ $ 

我不明白這一點。爲什麼會這樣? gdb有什麼明顯的我不知道?

據我最好的瞭解,p dictionary應該打印std::vector全局變量的內容,稱爲dictionary

而這不是第一次發生在我身上的事情。我記得gdb沒有看到函數,變量等。這次我決定記錄這個案例。

+0

它可能是'std :: dictionary',因爲那個很不明智的'using namespace std;'語句。 –

+0

@HenriMenke有沒有像'std :: dictionary'這樣的東西?我對這樣的事情一無所知,顯然cppreference.com [也不知道任何事情](https://www.google.pl/search?q=std::dictionary+site:en.cppreference。 com&gws_rd = cr&ei = iAT9WMfaIYO8swGjyp2YCw#q = std :: dictionary + site:en.cppreference.com&start = 0) – gaazkam

+0

無法重現。嘗試啓動'-g'到'-g3' – user4581301

回答

1

你可以看到這個問題,當您使用p di標籤標籤(像你一樣) - 由GCC產生的符號被稱爲「字典[ABI:cxx11]」,你可以通過輸入p 'dict打印標籤 - 請注意名稱周圍的引號以使gdb不嘗試將[ .. ]解釋爲表達式。

+1

但是,爲什麼這個符號稱爲'dictionary [abi:cxx11]',而不是簡單的'dictionary'?通常符號名稱沒有給出這樣的補充 – gaazkam

+0

@gaazkam g ++將pre C++ 11 ABI和post分開,但他們留下了使用其中一個或另一個的能力,因此命名的好處就是結果。 /gcc.gnu.org/onlinedocs/libstdc++/manual/api.html稍微多一些,可能是某個地方更好的答案,如果不是,這可能是一個正式的問題。 – user4581301

相關問題