Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Debug user space application with MMU

  1. #11
    Join Date
    Jul 2009
    Posts
    53
    Rep Power
    1

    Default Re: Debug user space application with MMU

    Hi Kazu,

    I have changed the shell to ash and now I can see the core file.

    Many thanks

  2. #12
    Join Date
    Aug 2009
    Posts
    204
    Rep Power
    1

    Default Re: Debug user space application with MMU

    This may be related to troubles debugging. I am trying to print a stack trace from within the program, but that doesn't work either. I tried with a very simple non-threaded C program:
    Code:
    #include <stdio.h>
    #include <execinfo.h>
    #include <stdlib.h>
    
    
    void print_trace() {
      void *array[10];
      size_t size;
    
      // get void*'s for all entries on the stack
      size = backtrace(array, 10);
    
      // print out all the frames to stderr
      fprintf(stderr, "trace size %zu:\n", size);
      backtrace_symbols_fd(array, size, 2);
      exit(1);
    }
    
    void baz() {
      print_trace();
    }
    
    void bar() { baz(); }
    void foo() { bar(); }
    
    int main(int argc, char **argv) {
      foo();
    }
    When I build this on my PC (Ubuntu 10.10 x86_64) I get a nice stacktrace:
    gcc -g -rdynamic -o test_bt test_bt.c
    Code:
    trace size 7:
    ./test_bt(print_trace+0x19)[0x4008fd]
    ./test_bt(baz+0xe)[0x400954]
    ./test_bt(bar+0xe)[0x400964]
    ./test_bt(foo+0xe)[0x400974]
    ./test_bt(main+0x19)[0x40098f]
    /lib/libc.so.6(__libc_start_main+0xfe)[0x7fc10f356d8e]
    ./test_bt[0x400829]
    On Nios II Linux I just get this:
    nios2-linux-gnu-gcc -g -rdynamic -o test_bt test_bt.c
    Code:
    trace size 1:
    [0x7fc43dcc]
    I've been trying to mess with compiler flags, but haven't found any magic so far. Any ideas?

  3. #13
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    28
    Rep Power
    1

    Default Re: Debug user space application with MMU

    Quote Originally Posted by ykozlov View Post
    I am trying to use a core file now. I am running `nios2-linux-gnu-gdb mynios2program`, with set solib-absolute-prefix /data/nios/nios2-linux/uClinux-dist/romfs/ in .gdbinit. Then core-file mycorefile, but all I can get is:
    Code:
    warning: Couldn't find general-purpose registers in core file.
    #0  0x00000000 in ?? ()
    I hit the same problem here too and traced it down to the kernel writing a faulty corefile, missing the .reg and .reg/<N> section. The cause for this was ELF_NGREG in arch/nios2/include/asm/elf.h being too small ((sizeof (struct pt_regs) / sizeof(elf_greg_t)), while ELF_CORE_COPY_REGS also writes all registers from struct switch_stack to the buffer.

    If ELF_NGREG is defined as in the following patch, the corefile is generated properly and can be used with nios2-linux-gnu-gdb:

    Code:
    -#define ELF_NGREG (sizeof (struct pt_regs) / sizeof(elf_greg_t))
    +#define ELF_NGREG ((sizeof(struct pt_regs) + sizeof(struct switch_stack)) / sizeof(elf_greg_t))
    I'll also apply this fix to the nios2 branch of the nios2-linux kernel git repository.

  4. #14
    Join Date
    Aug 2009
    Posts
    204
    Rep Power
    1

    Default Re: Debug user space application with MMU

    Tobias, I just saw this fix in the commit log and was surprised to see a link back to my report here because I missed your response in the thread. I will try this fix!

  5. #15
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    13
    Rep Power
    1

    Default Re: Debug user space application with MMU

    Quote Originally Posted by ykozlov View Post
    This may be related to troubles debugging. I am trying to print a stack trace from within the program, but that doesn't work either.
    Have you get it to work, ykozlov? I couldn't print out a stacktrace in a user-level program with backtrace() / backtrace_symbols()...

  6. #16
    Join Date
    Aug 2009
    Posts
    204
    Rep Power
    1

    Default Re: Debug user space application with MMU

    Quote Originally Posted by ABachmann View Post
    Have you get it to work, ykozlov? I couldn't print out a stacktrace in a user-level program with backtrace() / backtrace_symbols()...
    No, I never got that working, but tklauser's kernel fix did fix debugging using a core dump.

  7. #17
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    13
    Rep Power
    1

    Default Re: Debug user space application with MMU

    Quote Originally Posted by ykozlov View Post
    No, I never got that working, but tklauser's kernel fix did fix debugging using a core dump.
    And I fixed the backtrace() function... or bypass it and wrote my own!

    stackframe.c
    Code:
    #define _GNU_SOURCE
    
    #include <stdio.h>
    #include <inttypes.h>
    #include <stdint.h>
    #include <ucontext.h>
    #include <dlfcn.h>
    #include <string.h>
    
    struct frame {
        void *fp; // frame pointer
        void *ra; // return address
    };
    
    void print_symbol(void *address) {
        Dl_info info;
        char    library[128];
        char    function[128];
        void   *symbol_address;
    
        if (dladdr(address, &info) == 0) {
            strncpy(function, "???", sizeof(function));
            strncpy(library, "???", sizeof(library));
            symbol_address = 0;
        } else {
            strncpy(function, info.dli_sname, sizeof(function));
            strncpy(library, info.dli_fname, sizeof(library));
            symbol_address = info.dli_saddr;
        }
        printf("0x%08X %s: %s\n", (uint32_t) symbol_address, library, function);
    }
    
    void backtrace() {
        struct frame *frame;
    
        // frame = CURRENT_FRAME_POINTER
        __asm("mov %0,r28" : "=r"(frame));
    
        // Last frames
        while (frame && frame->fp) {
            printf("fp = 0x%08X ra = 0x%08X\n", (uint32_t) frame->fp, (uint32_t) frame->ra);
            print_symbol((void *) frame->ra);
            frame = (struct frame *) frame->fp;
        }
    }
    
    void test3() {
        backtrace();
    }
    
    void test2() {
        test3();
    }
    
    void test1() {
        test2();
    }
    
    int main(int argc, char *argv[]) {
        test1();
    
        return 0;
    }
    Makefile
    Code:
    CC             = nios2-linux-gnu-gcc
    CFLAGS         = -Wall -fno-omit-frame-pointer
    LDFLAGS        = -rdynamic -ldl
    
    SOURCES = stackframe.c
    OBJECTS = $(SOURCES:%.c=%.o)
    PROGRAM = stackframe
    
    .PHONY: all clean
    
    all: $(PROGRAM)
    
    clean:
        rm -rf $(OBJECTS) $(PROGRAM)
    
    build: clean all
    
    $(PROGRAM): $(OBJECTS)
        $(CC) -o $@ $(OBJECTS) $(LDFLAGS)
    
    %.o: %.c
        $(CC) $(CFLAGS) -c $<
    Build:
    Code:
    $ make
    nios2-linux-gnu-gcc -Wall -fno-omit-frame-pointer -c stackframe.c
    nios2-linux-gnu-gcc -o stackframe stackframe.o -rdynamic -ldl
    Execute:
    Code:
    [root@nios2 ~]# stackframe
    fp = 0x7FE81D24 ra = 0x00001838
    0x00001824 stackframe: test3
    fp = 0x7FE81D2C ra = 0x00001860
    0x0000184C stackframe: test2
    fp = 0x7FE81D34 ra = 0x00001888
    0x00001874 stackframe: test1
    fp = 0x7FE81D44 ra = 0x000018B8
    0x0000189C stackframe: main

Similar Threads

  1. Cant flash flash memory(user space)
    By Walther in forum General Discussion Forum
    Replies: 5
    Last Post: November 16th, 2009, 06:31 AM
  2. Accessing Hardware from user space
    By campo85 in forum Linux Forum
    Replies: 4
    Last Post: July 20th, 2009, 08:23 AM
  3. Flash user space in Stratix III dev kit
    By morph in forum Development Kit Related
    Replies: 0
    Last Post: March 11th, 2009, 03:06 AM
  4. Linux application debug problem
    By tgraessle in forum General Software Forum
    Replies: 5
    Last Post: February 16th, 2005, 07:48 AM
  5. How to debug linux application?
    By cloud in forum General Software Forum
    Replies: 1
    Last Post: December 16th, 2004, 10:29 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •