2

Sometimes I need to know size of a struct which is not in the scope (not even on the stack, i.e. frame-related commands won't help). E.g. it happens for debugging client + server communication, when restarting the apps to just break somewhere in context of the struct with the purpose of finding the size is uncomfortable and time consuming.

How do I find size of a struct defined in a header with disregard to my current context?

Hi-Angel
  • 4,933
  • 8
  • 63
  • 86
  • What about somethin like [Use gdb to call a printDebug method](http://stackoverflow.com/questions/6355196/use-gdb-to-call-a-printdebug-method): write a function which print out size of struct, rebuild your application and call that function just during debugging when you need info about the size? – Anto Jurković Mar 11 '15 at 11:28
  • 1
    @AntoJurković I'd then rather calculate it by hand. I couldn't write a method before for a particular struct because I don't know which one would be interesting to me. Of course I can write functions for all existing structs in app, but I don't think it would be worth it; also by the Merphi's rule most likely I won't be interested in these functions for a future debug. – Hi-Angel Mar 11 '15 at 12:56
  • 1
    Can you give an example of a program where a struct is defined in an #included file yet gdb isn't able to access it? They should almost always result in the structs being present in the global-level symbol table, except maybe for structs explicitly declared `static`. – Mark Plotnick Mar 11 '15 at 17:51

2 Answers2

10

For C, gdb's "expression language" is just ordinary C expressions, with a few handy extensions for debugging. This is less true for C++, primarily because C++ is just much more difficult to parse, so there expression language tends to be a subset of C++ plus some gdb extensions.

So, the short answer is you can just type:

(gdb) print sizeof(mystruct)

However, there are caveats.

First, gdb's current language matters. You can find this with show language. In the case of a struct type, in C++ there is an automatic typedef, but in C there is not. So if you are using the auto language (and you usually should), and are stopped in a C frame, you will need to use the keyword:

(gdb) print sizeof(struct mystruct)

Now, this still may not work. The usual reason at this point is that the structure isn't used in your program, and so doesn't show up in the debug info. The debug info can be optimized out even if you think it ought to have been available, because it is up to the compiler. For example, I think if a struct is only used in sizeof expressions (and no variable is ever defined of that type), then I think (hard to remember for sure) that GCC won't emit DWARF for it.

You can check to see if the type is available using readelf or dwgrep, like:

$ readelf -wi myexecutableorlibrary | grep mystruct

(Though in real life I usually use less and then examine the DWARF DIEs carefully. You will need to know a little DWARF to make sense of this.)

Sometimes in gdb it's handy to use the "filename" extension to specify exactly which entity you mean. Like:

(gdb) print 'myfile.c'::variable

Not sure if that works for types, and anyway it shouldn't usually be necessary for them.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • I suppose its pedantic, but there is another caveat in that C does not necessarily follow the one definition rule, so sizeof a type outside of a particular context may encounter multiple types, hopefully this is rare though – matt Mar 12 '15 at 02:52
  • Yeah, that's true. And the "::" extension is useful for that. Assuming it works -- I think it does but I haven't tried it lately and my memory... – Tom Tromey Mar 12 '15 at 03:17
  • @MarkPlotnick pointed out in comment that it looks like a mistake. They commented that all structs in headers mostly should end up in global namespace, i.e. i should be able to access it with disregard of how far my code execution gone at the moment I stopped it. Then I carefully examined the code, and noticed that actually it is, the error was just because a structs were in a separate namespace. Since it was a mistake, this answer would work too, so I am check it. – Hi-Angel Mar 12 '15 at 06:35
  • @Hi-Angel In my comment on your question, I didn't mean to imply that any mistakes were made. I just wanted to look at an example where gdb could not find a (other than static) struct declared in a header file, so that I could look into why it was happening. – Mark Plotnick Mar 12 '15 at 08:54
  • @MarkPlotnick but your simple question actually solved the problem; and when I am trying to imagine a code where structs wouldn't be seen globally, I have in mind either the case with a struct written directly in source file, either multiple headers included directly from a source file, neither of which seems to be a good style. – Hi-Angel Mar 12 '15 at 09:06
  • 1
    It's not entirely uncommon in C to have an incomplete struct in a header, like "struct whatever;", and then define it differently in two different source files. This isn't an error in C, just sometimes confusing, because in C there is no ODR (and since types don't have linkage). The gdb extension lets you cope with this. – Tom Tromey Mar 12 '15 at 14:34
0

In C/C++, you have the sizeof function which will give you the size of any type (including struct) or variable.

I'm not sure if you can apply this while debugging but you could simply have a test program with the same headers (type definitions) tell you what the size of your types is.

Matt Ko
  • 969
  • 7
  • 14
  • No, I need to know it not in C-code, but in the gdb which is debugging compiled C-code. Writing a test app with the same headers would take more time than just counting that by hand. – Hi-Angel Mar 11 '15 at 09:29
  • in that case, it's similar with gdb, take a look at this: http://stackoverflow.com/questions/5645027/can-gdb-print-the-size-of-structures-from-a-core-dump-of-a-c-program – Matt Ko Mar 11 '15 at 09:33
  • Yes, this one close. Unfortunately the commands `print sizeof(mystruct)` would lead to error `mystruct is not in current context` unless it is. – Hi-Angel Mar 11 '15 at 09:34
  • Variable is something that belongs to the context. When I need to print variable of course that works if the variable in context. But in my case I have no variable, just an abstract stucture whose size is only gdb knows. – Hi-Angel Mar 11 '15 at 09:40
  • FYI `print sizeof(mystruct) ` works with both variables and "abstract structures", but a structure have to be in context. I.e. the place where gdb execution stopped should have included header with that struct in source code. In my case I am often somewhere outside of these files which are include the *struct* I am interested in. That is, I am outside of context. – Hi-Angel Mar 11 '15 at 09:47
  • I'm not sure if gdb can do that then. In your case, this structure isn't necessarily anywhere in memory, so I don't think gdb can get any info on it. – Matt Ko Mar 11 '15 at 10:45