Arne
Arne

Reputation: 8481

segmentation fault when reading a value that is well defined in gdb

I have a big problem at the moment. I get a segmentation fault in my code, and I stepped through it with the debugger. From an object I am reading the member const char* name, to print it with printf. And here is where my segfault comes. I would guess that the pointer to the object is invalid, but from gdb I can inspect all values of that object, and the content of name is exactly what it should be. I have absolutely no idea, why I am getting a segmentation fault.

So what reasons can there be, that my program isn't allowed to read some data, while gdb is allowed to do so?

edit: for those people those people who still didn't get it, I am not asking for anyone to fix my problems, I just want to know how and in which scenarios a debugger is allowed to read some data, and my program is not. I added a screenshot as a proof that I am not making things up here, but I don't need you to understand my code.

Just some corner information. My program is a Plugin for a Qt Application. It uses the Qt plugin api to load plugins via lib<PluginName>.so.

g++ (Debian 4.9.2-10) 4.9.2 GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1

edit: I added a screenshot to better see the problem. The initialization is done it the constructor.

proof.

class PickingHandler {
public:
    const char* name;
    PickingHandler(const char* name) : name(name) {}
    [...]
    virtual void Drag(PickResult) = 0;
};

class ConnectHandler : public PickingHandler {
public:
    ConnectHandler() : PickingHandler("connect handler") {}
    [...]
    void Drag(PickResult) override;
};

Upvotes: 2

Views: 299

Answers (1)

Employed Russian
Employed Russian

Reputation: 213385

The most common reason (and the only reason I've ever seen this happen) why GDB can read the string, but the program can't, is when the data resides on a page without PROT_READ.

It can be considered a bug in the Linux kernel that ptrace(PEEK_DATA, ...) (system call which GDB uses to read the inferior (being debugged) program's data) succeeds when the address does not have read permissions and the program itself can't read that address.

You can use info proc maps in GDB, find the memory region which corresponds to (covers) plugin->picking_handler->name raw address, and see what its permissions are.

See also this answer.

Upvotes: 2

Related Questions