4

Consider a c++ program that produces some segmentation fault and aborted.

In regular debugging using gdb, I can do the following and see results

(gdb) r
(gdb) p str_var.size()

where str_var is defined as std::string in the file.

However, I encountered some problem when debugging with a core dump. After I load the core dump in gdb by

gdb EXECUTABLE core.pid

and run the following command in gdb terminal

(gdb) p str_var.size()

gdb says "You can't do that without a process to debug."

I am only able to do things like bt (view stack trace) or directly print std::string variable, but just couldn't find an easy way to check some information like printing the size of a std::string. Is it always the case that debugging capabilities are limited when debugging a core dump? Particularly for the problem here, is there a way to know the size of a std::string in core dump debugging?

linbianxiaocao
  • 910
  • 1
  • 12
  • 14
  • 1
    No, you need a process to run functions. Fortuately gdb gives you python pretty printers that can format std strings and containers nicely. – n. m. could be an AI Nov 26 '15 at 16:27
  • Very similar: [c - gdb evaluate function in process core - Stack Overflow](https://stackoverflow.com/questions/8892597/gdb-evaluate-function-in-process-core) – user202729 Mar 10 '22 at 01:00

2 Answers2

1

No, to be able to call a function, you need a context that is not present when reading core files. The only way to get you info is to explore the class(es) in order to find where it is stored, but can be painful as the stl can have some quite hard to follow implementations.

Moreover, depending on you program and compilation options, some values may be inlined (not stored anyway) or stored in registers, which can make the task quite painful.

Nevertheless, as said n.m, there are some pretty printers that ease the task if you use some standard stl (For example do not work on stlport AFAIK)

OznOg
  • 4,440
  • 2
  • 26
  • 35
  • Apart from pretty printer, gdb also has *xmethods* for precisely this task (should work on STL, and also Python scripting). – user202729 Mar 10 '22 at 01:00
0

If you compile with gcc, std::string struct: gcc std::string You should read _Rep before _M_dataplus._M_p.

p ((std::string::_Rep*)image_data)[-1]

And then print this.

{<std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep_base> = {_M_length = 30752, _M_capacity = 32711, _M_refcount = -1}, static _S_max_size = 4611686018427387897, static _S_terminal = 0 '\000', static _S_empty_rep_storage = <optimized out>}
ChienHo
  • 29
  • 2
  • your response doesn't include correct answer to question and consider translating your post to English – Adib Jul 31 '21 at 19:39