3

In the u-boot C code, the value "gd" is declared like this. (arm)

register volatile gd_t *gd asm ("r9")  

and register r9 contains the pointer to the struct global_data. (typedef'd to gd_t)

While debugging/analying, to see gd->malloc_base in the C code, typing "p gd->malloc_base" doesn't work. it says Missing ELF symbol "gd".
I later learned that I should do "p ((gd_t *)$r9)->malloc_base" to see the value. I'm typing this kind of command many times a day. Isn't there a way that I can assign a variable in gdb that represents ((gd_t *)$r9)? and why doesn't gdb recognize the value gd in the code?
What I want to do is make a variable representing ((gd_t *)$r9) so that I can use it like gd->ram_top or gd->env_has_init, etc. according to the value I'm curious about.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • Did you consider to use a GUI frontend to GDB? Commonly it provides "watches" or similar features that saves you from retyping. -- Another idea is to define a "user defined command" and call that. See gdb's documentation. -- And then there's the `display` command, but it might not be as helpful because it prints every time when the debuggee stops. – the busybee Oct 29 '21 at 06:24
  • yes, following your suggestion, I tried "ddd --debugger arm-none-eabi-gdb u-boot -command=gdbsetup_arm" and added "graph disp /x *((gd_t *)$9)" in the command. I can instantly see the changing values in the gd. This isn't a bad approach (the struct is a little big so graphics wath window should be taller, leaving the source window smaller. I tried 'user defined command' but what I want is to substitute the ((gd_t *)$r9) part only, changing the member variable according to the situation. But I think using ddd is quite good for me now. Thanks! (I updated my question) – Chan Kim Oct 29 '21 at 06:38
  • Most IDEs come with a debugger GUI, did you try this? -- Anyway, if you found your solution, feel free to post an answer of your own, and mark it, please. This will help others with the same issue. – the busybee Oct 29 '21 at 06:45
  • My comment was not exactly what I wanted. I'll just wait for a better answer. (replacing expression with a short variable in gdb). OK, I'll add an answer when no one comes up for a couple of days. :) – Chan Kim Oct 29 '21 at 07:02
  • 1
    If I needed something like this I would probably use [macro define](https://sourceware.org/gdb/current/onlinedocs/gdb/Macros.html#index-macro-define), like `macro define gd ((gd_t *)$r9)`. – ssbssa Oct 29 '21 at 12:43
  • @ssbssa Hi, this is the answer I was looking for. That works perfect. Please post your comment as an anser, I'll pick it as the selected answer. – Chan Kim Oct 29 '21 at 12:51

1 Answers1

0

This is the comment of @ssbssa to my quetion (thanks @ssbssa). I asked him to put his answer but he doesn't. so I add the answer myself.
You can define macro like macro define gd ((gd_t *)$r0) and then you can later use p /x gd->reloc_off or p /x gd->flags. Of course you can put this macro define command in the command file and start gdb like this arm-none-eabi-gdb u-boot -x gdb_command.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112