2012-02-29 3 views
7

gdb를 사용하여 C++ 프로그램을 디버깅하고 있습니다. 아르마딜로 수치 라이브러리를 사용하여 매트릭스를 정의하고 있습니다. 나는 이렇게 정의 된 아르마딜로 행렬을 가지고있다.gdb에 아르마딜로 행렬을 인쇄하는 방법이 있습니까?

mat A = randu<mat>(5,5); 

gdb 디버거를 사용하는 동안 전체 행렬을 인쇄 할 수 있습니까?

+0

최근 gdb (최신 릴리스는 GDB 7.4) can b e는 Python으로 스크립팅되었습니다. 그것을 고려 했습니까 ?? –

답변

2

gdb에서 C 함수를 호출 할 수 있으므로 개체를 인쇄하는 함수 만 있으면됩니다. 예 :

(gdb) call printf("%.2f", 3.1428) 
$7 = 4 
(gdb) call fflush(stdout) 
3.14$8 = 0 
7

질문이 오래되었을 수도 있지만 넘어지면서 내 자신의 작업에 대한 해결책을 찾을 수있었습니다.

아르마 라이브러리의 템플릿 기반의 특성으로 당신이 당신의 자신의 일부 헬퍼를 제공해야 인해 :

이제
#include <iostream> 
#include <armadillo> 

template<class Matrix> 
void print_matrix(Matrix matrix) { 
    matrix.print(std::cout); 
} 

//provide explicit instantiations of the template function for 
//every matrix type you use somewhere in your program. 
template void print_matrix<arma::mat>(arma::mat matrix); 
template void print_matrix<arma::cx_mat>(arma::cx_mat matrix); 

int main() { 
    arma::mat matrix = arma::randu(10,10); 

    return 0; 
} 

쉽게 gdb 내에서 print_matrix를 호출 할 수

(gdb) call print_matrix<arma::Mat<double> >(matrix) 
    0.8402 0.4774 0.0163 0.5129 0.5267 0.5260 0.2383 0.5316 0.6879 0.9565 
    0.3944 0.6289 0.2429 0.8391 0.7699 0.0861 0.9706 0.0393 0.1660 0.5886 
    0.7831 0.3648 0.1372 0.6126 0.4002 0.1922 0.9022 0.4376 0.4401 0.6573 
    0.7984 0.5134 0.8042 0.2960 0.8915 0.6632 0.8509 0.9318 0.8801 0.8587 
    0.9116 0.9522 0.1567 0.6376 0.2833 0.8902 0.2667 0.9308 0.8292 0.4396 
    0.1976 0.9162 0.4009 0.5243 0.3525 0.3489 0.5398 0.7210 0.3303 0.9240 
    0.3352 0.6357 0.1298 0.4936 0.8077 0.0642 0.3752 0.2843 0.2290 0.3984 
    0.7682 0.7173 0.1088 0.9728 0.9190 0.0200 0.7602 0.7385 0.8934 0.8148 
    0.2778 0.1416 0.9989 0.2925 0.0698 0.4577 0.5125 0.6400 0.3504 0.6842 
    0.5540 0.6070 0.2183 0.7714 0.9493 0.0631 0.6677 0.3540 0.6867 0.9110 

감사합니다 탭 완성을 위해 실제로는 print_matrix<arma::Mat<double> >의 몇 글자 만 입력하면됩니다.

1

QtCreator을 사용하는 경우 IDE에서 extending GDB with Python Debugging Helpers의 값을 검사 할 수 있습니다 (다른 IDE에서도이 기능을 지원할 수 있습니다).

장소 예에서 다음 스크립트, ~/debugHelpers.py

#!/usr/bin/python 

import gdb  # gdb.Value() 
import dumper # dumper.Children() 

def qdump__arma__Mat(d, value): 
    array = value["mem"] 
    cols = value["n_cols"] 
    rows = value["n_rows"] 
    maxDisplayItems = 50 
    innerType = d.templateArgument(value.type, 0) 
    p = gdb.Value(array.cast(innerType.pointer())) 
    d.putItemCount(cols) 
    d.putNumChild(cols) 
    if d.isExpanded(): 
     numDisplayItems = min(maxDisplayItems, cols) 
     with dumper.Children(d, numChild=cols, 
       maxNumChild=numDisplayItems, 
       childType="<column>", 
       addrBase=p, 
       addrStep=p.dereference().__sizeof__): 
      for i in range(0, int(numDisplayItems)): 
       with dumper.Children(d): 
        d.putItemCount(rows) 
        d.putNumChild(rows) 
        if d.isExpanded(): 
         numDisplayItems = min(maxDisplayItems, rows) 
         with dumper.Children(d, numChild=rows, 
           maxNumChild=numDisplayItems, 
           childType=innerType, 
           addrBase=p, 
           addrStep=p.dereference().__sizeof__): 
          for j in range(0, int(numDisplayItems)): 
           d.putSubItem(j, p.dereference()) 
           p += 1 

그리고 그것은 당신의 ~/.gdbinit이 줄을 추가 전화 :

python exec(open('/<full_path>/debugHelpers.py').read()) 

을하거나 IDE에서 추가; QtCreator에서 Tools> Options> Debugger> GDB (탭)> Extra Debugging Helpers (하단 근처)를 사용하십시오.

이 특정 스크립트 (내 건축 천연 메모리 배열) 컬럼으로 배열 행렬을 반환합니다

enter image description here

소스 : Writing Debug Visualizers for GDB/QtCreator 2.8

+0

QtCreator 3.4.0에서 작동하도록하는 데 문제가 있습니다. 모든 아르마딜로 매트릭스는'<접근 할 수 없음>'값을 얻습니다. 제 생각에는 gdb가 파이썬 3에 연결되어있는 것과 관련이 있다고 생각합니다. 어떤 제안이 있습니까? 나는'python exec (open (filename) .read())'를 사용하여 스크립트를 추가하고 있는데, 파이썬 3과 호환된다 ('execfile'은 그렇지 않다). –

+0

마침내 일부 작업을 마친 후에 스크립트가 작동하게되었습니다. 나는 최신 gdb (7.9.1)를 컴파일하고,'./configure --prefix/usr/local/gdb-python2 --with-python'을 사용하여 3 대신 파이썬 2에 연결하고, 그 gdb를 다음과 같이 사용했다. QtCreator. 또한'import gdb'와'from dumper import *'를'debugHelpers.py'의 맨 위에 추가해야했습니다.하지만 여전히 파이썬 3이 기본값으로 보이기 때문에 gdb와 파이썬 3에서 작동하는 스크립트 버전을 좋아할 것입니다. –

+1

이제'import gdb'와'import dumper'를 추가하고,'Children()'호출을'dumper.Children()'과'range (0, numDisplayItems)'호출로 변경합니다. 'to'range (0, int (numDisplayItems))'로 설정합니다. 전체 스크립트는 [여기] (http://pastebin.com/YjbJr4wv) (pastebin에 링크)에서 찾을 수 있습니다. –

0

가장 간단한 방법은 gdb에 직접 인쇄하는 것입니다, 불행하게도 멋진 형식 없음

> print *[email protected]@5 
$1 = {{0.84018771715470952, 0.39438292681909304,0.78309922375860586,0.79844003347607329, 0.91164735793678431}, 
{0.19755136929338396, 0.33522275571488902, 0.768229594811904, 0.27777471080318777, 0.55396995579543051}, 
{0.47739705186216025, 0.62887092476192441, 0.36478447279184334, 0.51340091019561551, 0.95222972517471283}, 
{0.91619506800370065, 0.63571172795990094, 0.71729692943268308, 0.14160255535580338, 0.60696887625705864}, 
{0.016300571624329581, 0.24288677062973696, 0.13723157678601872, 0.80417675422699042, 0.15667908925408455}} 
관련 문제