[Bug python/25234] New: Python pretty printer for C++ type fails when printing backtrace from a C function

jwakely.gcc at gmail dot com sourceware-bugzilla@sourceware.org
Fri Nov 29 12:26:00 GMT 2019


https://sourceware.org/bugzilla/show_bug.cgi?id=25234

            Bug ID: 25234
           Summary: Python pretty printer for C++ type fails when printing
                    backtrace from a C function
           Product: gdb
           Version: 8.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: python
          Assignee: unassigned at sourceware dot org
          Reporter: jwakely.gcc at gmail dot com
  Target Milestone: ---

tmp$ cat cpp.cc
#include <unordered_map>
extern "C" void print(int);

void foo(std::unordered_map<int, int> &map) {
  auto it = map.begin();
  print(it->first);
}

int main() {
  std::unordered_map<int, int> map;
  map[42] = 1;
  foo(map);
  return 0;
}
tmp$ cat c.c
#include <stdio.h>
void print(int i) {
  printf("%d\n", i);
}
tmp$ g++ -g cpp.cc -x c c.c 
tmp$ gdb -q -ex 'br print' -ex r -ex bt a.out
Reading symbols from a.out...
Breakpoint 1 at 0x402b89: file c.c, line 3.
Starting program: /tmp/a.out 

Breakpoint 1, print (i=42) at c.c:3
3         printf("%d\n", i);
#0  print (i=42) at c.c:3
#1  0x00000000004011f7 in foo (Python Exception <class 'IndexError'> list index
out of range: 
map=std::unordered_map with 1 element) at cpp.cc:6
#2  0x000000000040123b in main () at cpp.cc:12
(gdb) up
#1  0x00000000004011f7 in foo (map=std::unordered_map with 1 element = {...})
at cpp.cc:6
6         print(it->first);
(gdb) bt
#0  print (i=42) at c.c:3
#1  0x00000000004011f7 in foo (map=std::unordered_map with 1 element = {...})
at cpp.cc:6
#2  0x000000000040123b in main () at cpp.cc:12
(gdb) down
#0  print (i=42) at c.c:3
3         printf("%d\n", i);
(gdb) bt
#0  print (i=42) at c.c:3
#1  0x00000000004011f7 in foo (Python Exception <class 'IndexError'> list index
out of range: 
map=std::unordered_map with 1 element) at cpp.cc:6
#2  0x000000000040123b in main () at cpp.cc:12
(gdb) 

The error comes from the C++ pretty printer for parameter to the the
foo(std::unordered_map<int, int>&) function, which uses gdb.Type.fields() to
look at a base class:

        field = typ.fields()[0]

The printer keeps walking up the inheritance hierarchy until there are no more
base classes, which should never happen (in practice the function should return
before reaching the ultimate base class). It happens because an earlier call to
gdb.lookup_type(...) failed, but should have succeeded.

It seems that gdb.lookup_type is context-sensitive. Within the C++ frame it
finds the type, but within the C frame it doesn't.

When printing a C++ frame as part of the 'bt' output, lookup for C++ types
should work the same way as it does within that C++ frame.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the Gdb-prs mailing list