[英]How to output a HashMap element in GDB?
我只有一個二進制文件和一個核心轉儲,我需要在程序崩潰時使用 gdb 腳本返回 hashmap[8888][1] 中向量元素的值。 在程序運行時,容器被損壞,但聲稱可以獲得必要的數據。 問題是如何使用指針或其他東西來到達所需的向量並拉出我需要的元素?
(gdb) p hashmap
$1 = std::unordered_map with 5 elements
(gdb) ptype hashmap
type = std::unordered_map<int, std::vector<int>>
(gdb) p *(&hashmap - 2)
$2 = std::unordered_map with 94366596825088 elements = {[88888] = std::vector of length 3, capacity 3 = {1706628932, 1495493257, 18800028}, [8888] = std::vector of length 3, capacity 3 = {1254516933,
1257973910, 1938010846}, [888] = std::vector of length 3, capacity 3 = {1976864792, 130725599, 41716398}, [8] = std::vector of length 3, capacity 3 = {1533274456, 1112929722, 1834506749},
[88] = std::vector of length 3, capacity 3 = {1297327512, 118122261, 1973236149}}
通常, print hashmap
應該會生成您需要的所有 output。
例如,編譯這個程序:
#include <unordered_map>
#include <vector>
int main(int arch, char *argv[])
{
std::unordered_map<int, std::vector<int>> hm;
hm[7777].push_back(42);
hm[7777].push_back(24);
hm[8888].push_back(42);
return 0;
}
並在 GDB 下運行它會導致:
(gdb) p hm
$1 = std::unordered_map with 2 elements = {[8888] = std::vector of length 1, capacity 1 = {42}, [7777] = std::vector of length 2, capacity 2 = {42, 24}}
我們可以在您的 output 中看到 python 漂亮的打印機開始完成它的工作,但不知何故在句子中間失敗了。
發生這種情況的原因有很多,從gcc
或gdb
的錯誤版本到錯誤安裝的libstdc++
等等。
您的問題缺少所有相關細節。
首先,您應該嘗試從上述程序復制 output。 如果可行,請提供 GCC、GDB、libstdc++ 的版本,我們可以進一步猜測。
更新:
容器輕微損壞
啊,這是標准漂亮打印機無法工作的另一個原因。
我想了解如何使用 gdb 腳本將其拉出。
在 memory 損壞的情況下,您將不得不在原始 memory 中卑躬屈膝,沒有捷徑(甚至不能保證數據存在)。
您必須閱讀並理解std::unordered_map
的實現細節。 閱讀它的漂亮打印機(通常安裝到/usr/share/gdb/python/gdb/libstdcxx/v6/printers.py
)會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.