xcode GDB 问题:漂亮地打印二维数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4031957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:52:49  来源:igfitidea点击:

GDB question: Pretty-Printing a 2D Array?

c++objective-ccxcodegdb

提问by PlagueHammer

I have a 2d array matrix[10][10] that I'd like to inspect at debug time.

我有一个二维数组矩阵 [10][10],我想在调试时检查它。

I understand that I can do this in GDB using

我知道我可以在 GDB 中使用

p *matrix@10

But it outputs this in one line, making it difficult to read.

但是它在一行中输出它,使其难以阅读。

Is there a way to have this output formatted in any way, lets say as a matrix?

有没有办法让这个输出以任何方式格式化,比如矩阵?

回答by vitaut

An important feature of GDB is the ability of execute functions in the debugged code, so you can implement whatever printing you like, for example:

GDB 的一个重要特性是在调试代码中执行函数的能力,因此您可以实现您喜欢的任何打印,例如:

#include <stdio.h>

int matrix[10][10];

void print(int matrix[10][10])
{
    int i, j;
    for (i = 0; i < 10; ++i)
    {
        for (j = 0; j < 10; ++j)
            printf("%d ", matrix[i][j]);
        printf("\n");
    }
}

int main()
{
    int i, j;
    for (i = 0; i < 10; ++i)
        for (j = 0; j < 10; ++j)
            matrix[i][j] = i*10 + j;
    return 0;
}

After compiling this code with -g switch and running under GDB you can use the print function as follows:

使用 -g 开关编译此代码并在 GDB 下运行后,您可以按如下方式使用打印功能:

(gdb) call print(matrix)
0 1 2 3 4 5 6 7 8 9 
10 11 12 13 14 15 16 17 18 19 
20 21 22 23 24 25 26 27 28 29 
30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49 
50 51 52 53 54 55 56 57 58 59 
60 61 62 63 64 65 66 67 68 69 
70 71 72 73 74 75 76 77 78 79 
80 81 82 83 84 85 86 87 88 89 
90 91 92 93 94 95 96 97 98 99 

回答by alesplin

You could always define a function in GDB to do the same thing. If you had the following c code:

你总是可以在 GDB 中定义一个函数来做同样的事情。如果您有以下 c 代码:

#include <stdio.h>
#define ARRAY_SIZE 5

void printArray(int array[ARRAY_SIZE][ARRAY_SIZE]) {
  int y;

  for (y = 0; y < ARRAY_SIZE; y++) {
    printf("[%d,%d,%d,%d,%d]\n",
        array[y][0],
        array[y][1],
        array[y][2],
        array[y][3],
        array[y][4]);
  }
}

int main(int argc, char *argv[]) {
  int matrix[ARRAY_SIZE][ARRAY_SIZE] = {
    {1,2,3,4,5},
    {5,1,2,3,4},
    {4,5,1,2,3},
    {3,4,5,1,2},
    {2,3,4,5,1},
  };

  printArray(matrix);

  return 0;
}

You could then define the following GDB function (or something similar to it):

然后你可以定义以下 GDB 函数(或类似的东西):

(gdb) define printMatrix
Type commands for definition of "printmatrix".
End with a line saying just "end".
>set $arr = $arg0
>set $y = 0
>while $y < 5
 >printf "[%d,%d,%d,%d,%d]\n",$arr[$y][0],$arr[$y][1],$arr[$y][2],$arr[$y][3],$arr[$y][4]
 >set $y = $y + 1
 >end
>end

which would result in the following output:

这将导致以下输出:

(gdb) printMatrix matrix
[1,2,3,4,5]
[5,1,2,3,4]
[4,5,1,2,3]
[3,4,5,1,2]
[2,3,4,5,1]
(gdb) 

You could just as easily use nested while loops in your GDB function. And, as noted in a previous answer you can always just call the printArray function in your program from within GDB.

您可以轻松地在 GDB 函数中使用嵌套的 while 循环。而且,正如之前的回答中所述,您始终可以从 GDB 中调用程序中的 printArray 函数。

回答by user1530392

This is a more helpful extension of the last post. Also you can use: print var @cols@rows

这是对上一篇文章的更有用的扩展。您也可以使用:print var @cols@rows

define printMatrix
set $arr = $arg0
set $rows = $arg1
set $cols = $arg2
set $i = 0
printf "\n"
while $i < $rows
set $j = 0
while $j < $cols
printf "%8.4f,",$arr[$i*$cols + $j]
set $j = $j + 1
end
printf "\n"
set $i = $i + 1
end

回答by XYZ

Can you do this?:

你能做这个吗?:

p *((double (*)[3][3])pointerToMatrix3x3)

   (double [3][3])  = {
  [0] = ([0] = 1821.8790830216928, [1] = 0, [2] = 1622.4513098457062)
  [1] = ([0] = 0, [1] = 1172.3930433142486, [2] = 1314.4812787191868)
  [2] = ([0] = 0, [1] = 0, [2] = 1)

p *((double (*)[4])pointerToVector4)

  (double [3])  = ([0] = 1821.8790830216928, [1] = 0, [2] = 1622.4513098457062)

It works in lldb - Haven't tried it in gdb. It seems a lot easier.

它适用于 lldb - 尚未在 gdb 中尝试过。似乎轻松了许多。

回答by rne1223

You can also use try p (float[n][m])Arrayto print a 2d array in gdb.
For example, p (float[3][3])*C

您还可以使用 tryp (float[n][m])Array在 gdb 中打印二维数组。
例如,p (float[3][3])*C