Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB shows error message when trying to print a variable in an Assembly program

Tags:

assembly

gdb

yasm

While learning assembly language from a book there is a listing showing some basic operations:

segment .data
a   dq  176
b   dq  4097

segment .text
global _start

_start:
    mov rax, [a]    ; Move a into rax.
    add rax, [b]    ; add b o rax.
    xor rax, rax
    ret

After assembling with "$yasm -f elf64 -g dwarf2 -l listing.lst listing.asm" command and linking with "$ld -o listing listing.o" I ran the program in gdb. There whenever I tried to print the value of a variable, gdb showed this error message:

(gdb) p a
'a' has unknown type; cast it to its declared type

Same for the other variable 'b'. However casting 'a' or 'b' for int worked:

(gdb) p (int)a
$11 = 176
(gdb) p (int)b
$12 = 4097

But isn't this supposed to work without casting? Why do I need to cast? What mistake I've made in my source file?

like image 556
Anik Samiur Rahman Avatar asked Sep 10 '18 03:09

Anik Samiur Rahman


People also ask

How do you set a variable value in GDB?

The expression can be any expression that is valid in the current context. The set variable command evaluates the specified expression. If the expression includes the assignment operator ( = ), the debugger evaluates that operator, as it does with all operators in expressions, and assigns the new value.

What does print do in GDB?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages). expr is an expression (in the source language).

What does incomplete type mean in GDB?

It means that the type of that variable has been incompletely specified. For example: struct hatstand; struct hatstand *foo; GDB knows that foo is a pointer to a hatstand structure, but the members of that structure haven't been defined. Hence, "incomplete type".

How do you exit a function in GDB?

You can cancel execution of a function call with the return command. If you give an expression argument, its value is used as the function's return value. When you use return , GDB discards the selected stack frame (and all frames within it).


2 Answers

Older GDB used to default to assuming that a symbol was an int when it didn't have debug info describing the size / type.

This generally cause more confusion than the current behaviour, so it was changed. e.g. The value displayed in Kdbg is wrong -- NASM

like image 82
Peter Cordes Avatar answered Nov 08 '22 13:11

Peter Cordes


But isn't this supposed to work without casting?

No. GDB tells you that it has no idea what type a and b are.

What mistake I've made in my source file?

You didn't make any mistakes, but you also didn't supply any debugging info that GDB could use.

You may have expected yasm -g dwarf2 ... to do so, but it only creates minimal debug info describing the source, nothing else:

$ readelf -wi listing.o

Contents of the .debug_info section:

  Compilation Unit @ offset 0x0:
   Length:        0x37 (32-bit)
   Version:       2
   Abbrev Offset: 0x0
   Pointer Size:  8
 <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <c>   DW_AT_stmt_list   : 0x0
    <10>   DW_AT_low_pc      : 0x0
    <18>   DW_AT_high_pc     : 0x14
    <20>   DW_AT_name        : listing.asm
    <28>   DW_AT_comp_dir    : /tmp/
    <2e>   DW_AT_producer    : yasm 1.3.0
    <39>   DW_AT_language    : 32769    (MIPS assembler)
like image 41
Employed Russian Avatar answered Nov 08 '22 14:11

Employed Russian