2

Please see how gdb fails to print the global variable dictionary in the code below:

m@m-X555LJ ~ $ 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());
  }
}
m@m-X555LJ ~ $ g++ -g -std=c++11 -o 148 148.cc
m@m-X555LJ ~ $ 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
#
m@m-X555LJ ~ $ 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
m@m-X555LJ ~ $ 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.

m@m-X555LJ ~ $ 

I do not understand this. Why is this the case? Is there something obvious about gdb I do not know?

To my best understanding, p dictionary should print the contents of the std::vector global variable called dictionary.

And this is not the first time something like that happens to me. I remember gdb not seeing functions, variables, etc. This time I decided to record the case.

ks1322
  • 33,961
  • 14
  • 109
  • 164
  • It might be `std::dictionary` because of that very ill-advised `using namespace std;` statement. – Henri Menke Apr 23 '17 at 19:45
  • @HenriMenke Is there such thing as `std::dictionary`? I don't know anything about such a thing, and apparently cppreference.com [doesn't know anything either](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) –  Apr 23 '17 at 19:47
  • Unable to reproduce. Try cranking the `-g` up to `-g3` – user4581301 Apr 23 '17 at 20:01
  • @user4581301 Not helpful, sadly :( I still get the issue. –  Apr 23 '17 at 20:09
  • Possible duplicate of [can not print global objects in gdb](http://stackoverflow.com/questions/40820535/can-not-print-global-objects-in-gdb) – ks1322 Apr 24 '17 at 09:24

1 Answers1

1

You can see the problem when you use p ditabtab (as you did) -- the symbol produced by gcc is called "dictionary[abi:cxx11]", which you can print by entering p 'dicttab -- note the quotes around the name to get gdb to not try to interpret the [..] as an expression.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    Thank you. But, but, why is this symbol called `dictionary[abi:cxx11]`, and not simply `dictionary`? Usually symbol names are not given such additions. –  Apr 23 '17 at 20:17
  • @gaazkam g++ split the pre C++11 ABI and the post. But they left the ability to use one or the other, this fun with naming is a result. Skip to the end of https://gcc.gnu.org/onlinedocs/libstdc++/manual/api.html for wee bit more. May be a better answer on SO somewhere. If not, This is probably worth a formal question. – user4581301 Apr 23 '17 at 20:27