generic way to print out variable name in c++

前端 未结 6 757
难免孤独
难免孤独 2020-12-05 20:47

given a class

struct {
  int a1;
  bool a2;
  ...
  char* a500;
  ...
  char a10000;      
}

I want to print or stream out



        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 21:48

    GDB can print structs. This script generates gdb script to set breakpoints and print values at specified by gdb_print locations:

    gdb-print-prepare()
    {
    
        # usage:
        # mark print points with empty standalone defines:
        # gdb_print(huge_struct);
        # gdb-print-prepare $src > app.gdb
        # gdb --batch --quiet --command=app.gdb $app
        cat  <<-EOF
        set auto-load safe-path /
        EOF
        grep --with-filename --line-number --recursive '^\s\+gdb_print(.*);' $1 | \
        while IFS=$'\t ;()' read line func var rest; do
            cat  <<-EOF
            break ${line%:}
            commands
            silent
            where 1
            echo \\n$var\\n
            print $var
            cont
            end
            EOF
        done
        cat  <<-EOF
        run
        bt
        echo ---\\n
        EOF
    }
    

    From https://gitlab.com/makelinux/lib/blob/master/snippets/gdb-print-prepare.md

提交回复
热议问题