0

How can I get line numbering when running address sanitizer on gcc?

I have a simple program to test address santizer (buffer overflow):

#include<stdio.h>


int main(void)
{
    int A[10] = {0};
    A[10] = 1;

}

when running address sanitizer (gcc):

gcc b_oflow.c -fsanitize=address -g 
==7961==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff90e56898 at pc 0x564401bc42be bp 0x7fff90e56840 sp 0x7fff90e56838
WRITE of size 4 at 0x7fff90e56898 thread T0
    #0 0x564401bc42bd in main (/home/victor/Documents/code/C/lisp/src/a.out+0x12bd)
    #1 0x7fbeb1a7009a in __libc_start_main ../csu/libc-start.c:308
    #2 0x564401bc40b9 in _start (/home/victor/Documents/code/C/lisp/src/a.out+0x10b9)

Address 0x7fff90e56898 is located in stack of thread T0 at offset 72 in frame
    #0 0x564401bc4184 in main (/home/victor/Documents/code/C/lisp/src/a.out+0x1184)

How can I get to output the line numbering? I've tried answers from questions: How do I get line numbers in the debug output with clang's -fsanitize=address? and How do I get line numbers in the debug output with clang's -fsanitize=address? but don't know what to do, I wanted to print the line where the overflow occurred when running the executable.

  • 1
    Usually, add the `-g` option to both the compilation to object file and the linking process. – Jonathan Leffler Jul 26 '21 at 06:19
  • I've tried with -g now and continues to print the address but not line numbering. – victor 0x3E7 Jul 26 '21 at 06:22
  • Which version of GCC are you using? – Some programmer dude Jul 26 '21 at 06:31
  • gcc (Debian 8.3.0-6) 8.3.0 – victor 0x3E7 Jul 26 '21 at 06:32
  • 2
    I can't replicate your problem [in the compiler explorer](https://godbolt.org/z/eWWbEzxo7). Even *without* the `-g` option I get line-numbers. Only if I add the `-g0` option (to explicitly disable all debug information) won't I get it. The `-g` option should really make sure you get the line-number. – Some programmer dude Jul 26 '21 at 06:35
  • 1
    I cannot reproduce. I copied the file and command as-is and it worked. `../csu/libc-start.c:308` - Are you using some LD_PRELOAD? Or specifying custom configuration with custom startup? Your compiler should have linked with `libc.so.6`. Please post the output of `gcc -v ....`. Please try compiling and running in a new fresh shell. Please try compiling with `env -i PATH=$PATH gcc ...` and then running with `env -i ./a.out`?? – KamilCuk Jul 26 '21 at 06:55
  • using gcc -v I get: https://pastebin.com/BHJGhWZA ,but using your options I still get no line numbers,only addresses. – victor 0x3E7 Jul 26 '21 at 12:00
  • Have you found a solution to this particular problem? – llambdaa Jan 13 '22 at 16:08

0 Answers0