1

I'm trying to track down a segfault in a c++ program. I noticed that argc has been modified to a very large number at the time of the segfault. The number happens to be an address in the address space of my application. This led me to believe that something is corrupting my main stack frame. The question is, how do I get the address on the stack which points to argc, so that I can tell what method is modifying the argument in my program? I want to step through the program and watch that address space for changes. This segfault is happening on hpux and aix (right now I'm looking at hpux). I've been looking through the stack, but I can't find my argc variable pushed anywhere onto the stack. I'm debugging with gdb.

Does anyone know where argc would be stored on the stack frame of an hpux pa-risc machine?

David Mulder
  • 7,595
  • 11
  • 45
  • 61
  • I expected the variable to be right near the bottom of the stack, but I don't see it there. – David Mulder Jan 23 '14 at 23:39
  • 1
    Place a breakpoint near the start of your `main` and your debugger should be able to provide you with the address (a good debugger will also let you watch the memory location). – Zac Howland Jan 23 '14 at 23:41
  • If you can debug and your debugger has any sense at all a break-on-write should be established on the address of `argc`, which a stack dump should tell you at program startup. When all else fails `int *p = &argc;` set the bow-bp on whatever address is in `p`. Then just let it run and when its overwritten, you'll be told immediately. – WhozCraig Jan 23 '14 at 23:42
  • This is what I'm looking for: http://stackoverflow.com/questions/6511560/can-i-have-gdb-break-on-read-write-from-an-address – David Mulder Jan 23 '14 at 23:46
  • A gdb watch would work, except it becomes invalid as soon as I leave the main method. So, I still need to know the address argc is located. gdb doesn't tell me the address of argc, only it's value. – David Mulder Jan 23 '14 at 23:50
  • gdb will tell you the address if you tell it to, do `p &argc` when you're in main() – nos Jan 23 '14 at 23:51
  • Thanks nos. Apparently hpux pa-risc stores argc in a register, not in memory (which gdb informed me when I did p &argc). – David Mulder Jan 23 '14 at 23:53
  • 1
    So, I guess I was barking up the wrong tree. argc only changed because hpux pa-risc reassigned the the register because it wasn't being used. On linux, argc is stored on the stack (I just checked). – David Mulder Jan 23 '14 at 23:57
  • @David: post this as an answer and I'll upvote. – LThode Nov 07 '14 at 21:17
  • @LThode There you go. – David Mulder Nov 11 '14 at 20:30

1 Answers1

1

Apparently hpux pa-risc stores argc in a register, not in memory (which gdb informed me when I did p &argc). argc only changed because hpux pa-risc reassigned the the register because it wasn't being used. On linux, argc is stored on the stack.

David Mulder
  • 7,595
  • 11
  • 45
  • 61