Can't debug: assert error with gdb (no problem without)


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Programming Can't debug: assert error with gdb (no problem without)
# 1  
Can't debug: assert error with gdb (no problem without)

I'm sorry if the title is really criptic, but I don't know how to phrase my problem.

I know I can't really ask for a solution, and I normally wouldn't but this is really escaping my abilities.

Antefacts.
I developed a program using the zeromq messaging library.
I got to a point where the program works and then started going multithreading: moving a message poller outside of the main thread.
This is supported by the library.
So I devised my thread, passed it a pointer to some parameters (including the pointer to the zmq context) and went ahead.

The Problem
The code in the child thread is the same code I had in the parent, save for the facts that functions parameters are stored in a structure that gets passed to the child.
I followed every function in the child and the parameters are correct.
The code does even work (as it should) when run normally, but hangs with an assert when run in debug.

I tried following every function I could, built the library with debug symbols, using gdb from the console...

I got nothing useful.
I still don't understand why it fails with the debugger attached, and why it runs without.

The culprit code is the following
Code:
        if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
             pgm_error->domain == PGM_ERROR_DOMAIN_IF) && (
             pgm_error->code != PGM_ERROR_INVAL &&
             pgm_error->code != PGM_ERROR_BADF &&
             pgm_error->code != PGM_ERROR_FAULT))

from pgm_socket.cpp:301
And here are a couple gdb outputs
Code:
Breakpoint 1, zmq::pgm_socket_t::init (this=0x7ffff0000930, 
    udp_encapsulation_=true, network_=<optimized out>) at pgm_socket.cpp:301
warning: Source file is more recent than executable.
301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) stepi
0x00007ffff786488d    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff786488f    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff7864891    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff7864894    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff7864896    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff7864899    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff786489c    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff786489e    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff78648a1    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff78648a3    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
0x00007ffff78648a6    301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) 
376        if (sock != NULL) {
(gdb) 



Code:
Breakpoint 1, zmq::pgm_socket_t::init (this=0x7ffff0000930, 
    udp_encapsulation_=true, network_=<optimized out>) at pgm_socket.cpp:301
301            if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
(gdb) step
376        if (sock != NULL) {
(gdb) 
377            pgm_close (sock, FALSE);
(gdb) 

There is supposedly no way (and no reason) to jump from line 301 to 376, yet it happens.
After that there is a
Code:
return -1;

that triggers the assert that kills my code.

And I'm banging my head over this without so much as a hope of understanding what to do.

I can't really hope for a solution with this, but at least a suggestion as to how to proceed.

My code (if anyone really wanted to check it out) on github is under the project "saettang".
# 2  
Stack corruption might be a possibility.

Without seeing the code, we can't possibly say.
# 3  
, I can post the code no problem, this is the smallest minimal example iI couldd devise.
Code:

(android won't let me paste the code... I'll have to get to my computer.
Although even when I'll be able to show you the code, you'll still need the library cited above http://github.com/erupter/saettang
# 4  
What value do pgm_error->domain and pgm_error->code have?
# 5  
Quote:
Originally Posted by kbw
What value do pgm_error->domain and pgm_error->code have?
No matter how I compiled the relative library, the values are optimized out.
I tried explicitly configuring with -O0 but to no avail.

Here is my minimal test code.
It requires the Zmqcpp library found in my github project, as well as ZeroMQ library 3.2.2 compiled with pgm support.

Code:
/* 
     * File:   main.cpp
     * Author: erupter
     *
     * Created on January 9, 2013, 2:03 PM
     */
    
    /* 
     * File:   main.cpp
     * Author: erupter
     *
     * Created on January 9, 2013, 2:03 PM
     */
    
    #include <cstdlib>
    #include <net/if.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/ioctl.h>
    #include <list>
    #include <zmqcpp.h>
    #define THREAD_CONTROL_IPC      "ipc://saetta_th_control"
    #define MULTICAST_ADDRESS       "239.192.1.1"
    #define MULTICAST_PORT          5678
    #define ROUTER_PORT             5679
    #define HEADER_CTRL             "CTRL"
    #define HEADER_SERVER_INFO      "SERVER_INFO"
    
    using namespace std;
    std::string _local_ip_address;
    std::stringstream ss;
    // custom struct to hold parameters to pass to child threads
    struct parameters{
        // pointer to context
        Zmqcpp::Context * zmqcont;
        // function pointer for callbacks
        void (*callback) (std::string);
        //ip string to connect to
        std::string ip;
        //if child should bind or connect
        int conntype;
        //topics to subscribe
        std::list<std::string> topics;
    };
    
    //child thread
    void *th_subscriber (void * params);
    //callback
    void server_location_callback(std::string ip);
    //service function to get the specified interface's address
    static inline bool get_iface_address(std::string interf);
    /*
     * 
     */
    int main(int argc, char** argv) {
    
        struct parameters myparams;
        printf( "Requested interface wlan0, attempting to fetch     address...\n");
        if (!get_iface_address("wlan0"))
            return (EXIT_FAILURE);
    
        //create the pgm address chaining various parameters defined     above plus the local ip address of the interface
        ss << "epgm://" << _local_ip_address << ";"     << MULTICAST_ADDRESS << ":" << MULTICAST_PORT     << std::endl;
    
        //create context
        Zmqcpp::Context* mycontext = new Zmqcpp::Context(1);
        
        //fill the parameters structure
        myparams.ip = ss.str();
        ss.str("");
        ss.str(HEADER_SERVER_INFO);
        myparams.topics.insert(myparams.topics.end(),ss.str());
        myparams.conntype = ZMQCPP_CONNECT;
        myparams.zmqcont=mycontext;
        myparams.callback=&server_location_callback;
        
        //create the connection string for the control socket
        ss.str("");
        ss << THREAD_CONTROL_IPC;
        //create the control socket
        Zmqcpp::Publisher *ctrlpublisher = new     Zmqcpp::Publisher(mycontext, THREAD_CONTROL_IPC, ZMQCPP_BIND);
        
        //create the child thread
        pthread_t th_hndl_subber;
        pthread_create(&th_hndl_subber, NULL, th_subscriber, (void     *)&myparams);
        
        
        while(1)
        {        
            //send control messages over the control socket
            ctrlpublisher->PubMsg(2,HEADER_CTRL,"Test");
            sleep(1);
        }
    
    
        //garbage collection
        void *status;
        pthread_join(th_hndl_subber, &status);
        printf("Main thread cleaning.\n");
        ctrlpublisher->~Publisher();
        mycontext->~Context();
        printf("Main thread cleaned succesfully.");
        return (EXIT_SUCCESS);
    }
    
    static inline bool get_iface_address(std::string interf)
    {
        int s;
        struct ifreq ifr = {};
        s = socket(PF_INET, SOCK_DGRAM, 0);
    
        strncpy(ifr.ifr_name, interf.c_str(), sizeof(ifr.ifr_name));
    
        if (ioctl(s, SIOCGIFADDR, &ifr) >= 0)
        {        
            _local_ip_address = inet_ntoa(((struct sockaddr_in     *)&ifr.ifr_addr)->sin_addr);
            printf("Detected interface IP address is %s.\nCreating     sockets on this address.\n", _local_ip_address.c_str());          
            return true;
        }    
        printf("Unable to get interface IP address: is the interface     configured?\n");
        return false;
        
    }
    
    void *th_subscriber (void * params)
    {
        //declare some service variables
        int th_continue=1;
        std::string server_ip;
        std::stringstream ss;
        std::string msgtype;
        std::string msgmaster = HEADER_SERVER_INFO;
        std::string temp;
    
        //recast the pointer to the parameter structure
        struct parameters *myparams = (struct parameters *) params;
    
        //create the subscriber for the real messages we would like to     get
        // THIS IS WHAT TRIGGERS THE ASSERT DURING DEBUG
        Zmqcpp::Subscriber *msgsubber = new     Zmqcpp::Subscriber(myparams->zmqcont,&(myparams->ip),     myparams->conntype);
    
        //have it subscribe to each topic we are told to
        for (std::list<std::string>::iterator it =     myparams->topics.begin(); it != myparams->topics.end(); it++)
        {
            cout << "Subscribing to <" << *it     <<">"<<std::endl;
            printf("Subscribing to: <%s>\n",it->c_str());
            msgsubber->SubscribeTopic(*it);
        }
    
        
        ss.str("");
        ss << THREAD_CONTROL_IPC;
    
        //create the control subscriber over the specified ipc control     address and subscribe it
        Zmqcpp::Subscriber *ctrlsubber = new     Zmqcpp::Subscriber(myparams->zmqcont,THREAD_CONTROL_IPC,     ZMQCPP_CONNECT);
        ctrlsubber->SubscribeTopic(HEADER_CTRL);
        
        sleep(1);
        while(th_continue==1)
        {
            
            //do something silly with the control messages
            ss.str("");
            ss << ctrlsubber->RecvMsg();
            msgtype.clear();
            msgtype = ss.str();
            if (!msgtype.compare(HEADER_CTRL))
            {
                ss.str("");            
                ss << "Control message: " <<     ctrlsubber->RecvMsg();
                temp.clear();
                temp.assign(ss.str());
                myparams->callback(temp.c_str());
                if (temp.compare("Control message: KILL")==0)                
                    th_continue=0;
                    
            }
        }
        //child thread garbage collection
        printf("Child thread cleaning resources.\n");
        ctrlsubber->~Subscriber();
        //msgsubber->~Subscriber();
        printf("Child thread terminating.\n");
        return(EXIT_SUCCESS);
        
    }
    
    void server_location_callback(std::string ip)
    {
        
        cout << ip.c_str() <<  std::endl;    
    }

If you literally cut&paste the msgsubscriber inside the main thread, obviously modifying the passed parameters, you can both run and debug the code.
As it is you can't.

Last edited by erupter; 01-11-2013 at 09:22 AM..
# 6  
Is this relevant to your example? (post #1)

warning: Source file is more recent than executable.

Just because gdb shows line #1 of a multiline construct does not mean that line #1 of your if block is the problem.

If you actually want to debug it, break the if's into singletons(this is logical decompostion to monads, so by a theorem by Courcelle):

Code:
 int do_this=0;
 if (pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET)
 {     
    if(pgm_error->code != PGM_ERROR_INVAL)
    {
       if(pgm_error->code != PGM_ERROR_BADF )
       {
           if(pgm_error->code != PGM_ERROR_FAULT)
             do_this=1;
       }
 }            
 if(pgm_error->domain == PGM_ERROR_DOMAIN_IF) 
 {     
    if(pgm_error->code != PGM_ERROR_INVAL)
    {
       if(pgm_error->code != PGM_ERROR_BADF )
       {
           if(pgm_error->code != PGM_ERROR_FAULT)
             do_this=1;
       }
 }            
 if(do_this)
 {
     original code from inside  if() block goes here 
 }

# 7  
Quote:
Originally Posted by jim mcnamara
Is this relevant to your example? (post #1)

warning: Source file is more recent than executable.
Not really, I had modified the phrasing of the if to see if gdb behaved any differently. It didn't.
Quote:
Originally Posted by jim mcnamara
Just because gdb shows line #1 of a multiline construct does not mean that line #1 of your if block is the problem.

If you actually want to debug it, break the if's into singletons(this is logical decompostion to monads, so by a theorem by Courcelle):
That's not exactly the problem: the if gives way to a goto statement.
But if any of the conditions triggered the conditional to go fordward, gdb ought to show the call to the goto instruction... at least for what I know.
It instead shows the program as jumping directly to the first line after the goto label.

If then it isn't so, I'll separate the various ifs.


I tried your suggestion anyway.
Code:
        if (pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET)
        {     
           if(pgm_error->code != PGM_ERROR_INVAL)
           {
              if(pgm_error->code != PGM_ERROR_BADF )
              {
                  if(pgm_error->code != PGM_ERROR_FAULT)
                    do_this=1;
              }
           }
        }            
        if(pgm_error->domain == PGM_ERROR_DOMAIN_IF) 
        {     
           if(pgm_error->code != PGM_ERROR_INVAL)
           {
              if(pgm_error->code != PGM_ERROR_BADF )
              {
                  if(pgm_error->code != PGM_ERROR_FAULT)
                    do_this=1;
              }
           }
        }            
        if(do_this)
        {
            goto err_abort;
        }

And here is the output of gdb, i interleaved the step instructions with info locals to show the value of do_this
Code:
Breakpoint 1, zmq::pgm_socket_t::init (this=0x7ffff0000930, 
    udp_encapsulation_=true, network_=<optimized out>) at pgm_socket.cpp:311
311            if (pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET)
(gdb) info locals
port_number = 5678
sa_family = 2
pgm_error = 0x7ffff0000f40
buf = {3829176425, 4105231441}
if_req = {ir_interface = 3, ir_scope_id = 0}
do_this = 0
res = 0x7ffff0001110
addr = {sa_port = 5678, sa_addr = {gsi = {
      identifier = "\310\362\033\036\237\200"}, sport = 0}}
(gdb) step
Too many open files (signaler.cpp:230)
313               if(pgm_error->code != PGM_ERROR_INVAL)
(gdb) info locals
port_number = 5678
sa_family = 2
pgm_error = 0x7ffff0000f40
buf = {3829176425, 4105231441}
if_req = {ir_interface = 3, ir_scope_id = 0}
do_this = 0
res = 0x7ffff0001110
addr = {sa_port = 5678, sa_addr = {gsi = {
      identifier = "\310\362\033\036\237\200"}, sport = 0}}
(gdb) step
315                  if(pgm_error->code != PGM_ERROR_BADF )
(gdb) info locals
port_number = 5678
sa_family = 2
pgm_error = 0x7ffff0000f40
buf = {3829176425, 4105231441}
if_req = {ir_interface = 3, ir_scope_id = 0}
do_this = 0
res = 0x7ffff0001110
addr = {sa_port = 5678, sa_addr = {gsi = {
      identifier = "\310\362\033\036\237\200"}, sport = 0}}
(gdb) step
333            if(do_this)
(gdb) 
405        if (sock != NULL) {
(gdb)

As you can see up to the last instruction do_this is zero, yet the jump happens anyway.

Last edited by erupter; 01-11-2013 at 01:19 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #386
Difficulty: Medium
The Linux telnet command provides a user interface to a remote system using the SSH protocol.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Odd behavior from GDB while trying to cross-debug an embedded Linux application.

Some background: The application normally runs on an embedded platform. Currently, for development purposes, I have the rootfs located @ /exports and the target is communicating over NFS. That way I can make a change on my local system, save the application @ /exports, and run the altered... (4 Replies)
Discussion started by: Circuits
4 Replies

2. Programming

GDB problem accessing static variables in C

Hi, Can anyone explain this please..... This is on AIX with GDB (tried a few versions). It only happens when program compiled in 64 bit and only with static variables.... A simple test program... ### snip #include <stdio.h> main() { static int n; n = 6; printf("hello %d\n", n);... (0 Replies)
Discussion started by: bagpussnz
0 Replies

3. UNIX for Dummies Questions & Answers

GDB Debugging Problem

I have added some code in my file. I have created executable rpm file of our code and also I have created debuginfo and debugsource files and installed all three. But when I debug in gdb I see the the code changes in soucre file. But the break point does not hit at that place as if it did not... (1 Reply)
Discussion started by: rupeshkp728
1 Replies

4. Programming

ddd/gdb debugger problem

I hope this is the right thread - not sure if it belongs in the Linux forum. Anyway, I'm having an issue with ddd and gdb. When using ddd to run gdb, there are extra parameters being appended, and my program is picking them up. I'm running on Ubuntu 11.04... Here's my command line entry - ... (1 Reply)
Discussion started by: jalburger
1 Replies

5. Programming

Debug two process Using GDB

Hi All I know How to attach a process to beubg it .But for my application I am using client as well server.Both are two separate process .Suppose I need to debug both .How to attach both of them together .Or I have to attach them separetly . Suppose client process id is 1325 and server is... (2 Replies)
Discussion started by: mr_deb
2 Replies

6. UNIX for Advanced & Expert Users

a question about debug using gdb(/lib32/libc.so.6: No such file or directory)

Hi all, (gdb) target remote 192.168.185.196:1389 Remote debugging using 192.168.185.196:1389 warning: Unable to find dynamic linker breakpoint function. GDB will be unable to debug shared library initializers and track explicitly loaded dynamic code. 0x2fbd0800 in ?? () (gdb) l 1 ... (0 Replies)
Discussion started by: yanglei_fage
0 Replies

7. Programming

gdb: problem while debug an executable file

I created one file (test.cpp)and complied it and get the executable file(test) and run it.It is giving the out put but when I am trying to debug that executable.I am getting the error -- please tell me the solutions?? Thanks in advance ..... (2 Replies)
Discussion started by: smartgupta
2 Replies

8. Programming

gdb: how to debug an executable file with a few command line papameters

If an executalbe file has several parameters, gdb can not pass parameters correctly. Let us see: run: ./executablefile1 agr1 arg2 arg3 debug: gdb executablefile1 run executalbefile1 arg1 arg2 arg3 then argv : executablefile1 argv : executablefile argv : arg1 ... (3 Replies)
Discussion started by: cdbug
3 Replies

9. Programming

GNU GDB compile problem

I have to compile gnu-gdb 6.7 on HP-UX 11.11 with /usr/local/pa64/bin/gcc 64-bit compiler, but I'm having some problems during "make": ser-tcp.c: In function `net_open': ser-tcp.c:207: warning: passing arg 5 of `getsockopt' from incompatible pointer type make: *** Error 1 make: Leaving... (8 Replies)
Discussion started by: untamed
8 Replies

10. HP-UX

gdb problem viewing variables

Hi, I don't know if this is HP-UX specific, but I'll take my chances with this forum. ;) I'm using WDB 5.7 on HP Itanium 11.23. 'file' on my executable shows: ELF-32 executable object file - IA64 The problem is with viewing the contents of the variables. Following is a sequence that... (0 Replies)
Discussion started by: rimon
0 Replies

Featured Tech Videos