Compiling mongo-c-driver, mongo-cxx-driver with gdb support

57 views
Skip to first unread message

Jeff Abrahamson

unread,
Jul 20, 2018, 10:58:25 AM7/20/18
to mongodb-user

I'm seeing a segfault from mongcxx::client(uri, options).  I want to understand what's happening, both to fix my code and to report a bug to the appropriate devs.  When I look at the mongocxx code, I don't think a segfault should be possible.  So I want to compile a version of mongo-c-driver and mongo-cxx-driver with gdb support.

Alas, I don't see how to do that.  (Note: I already compile both from git HEAD, so this shouldn't be too hard here if only I can find the right flag.)

Sorry if this is obvious.  I'm not too facile with cmake, and I've not found the answer from googling or poking through code.

Thanks for any tips.

-- 

Jeff Abrahamson
+33 6 24 40 01 57
+44 7920 594 255

http://p27.eu/jeff/

Andrew Morrow

unread,
Jul 20, 2018, 11:09:05 AM7/20/18
to mongod...@googlegroups.com

Hi Jeff -

CMake knows how to do this for you. I'm assuming a UNIX like environment here since you said you are trying to use GDB. To get a debug build you should build both the C driver and CXX driver with CMAKE_BUILD_TYPE explicitly set. You should set it to Debug for a non-optimized build containing debug info or RelWithDebInfo to get an optimized build with debugging info. On the command line, that will look like:

cmake -DCMAKE_BUILD_TYPE=Debug <your-other-cmake-args...>

Please give that a try and let us know what you find out. Some other options, are to run your binaries under valgrind. Sometimes that will make an underlying memory error leading to a segfault almost immediately clear. You can also try Address Sanitizer, though that is a little harder to set up with CMake in my experience.

Thanks,
Andrew

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/c0d30027-9803-890c-4c7a-18ab22d807b3%40p27.eu.
For more options, visit https://groups.google.com/d/optout.

Jeff Abrahamson

unread,
Jul 20, 2018, 1:10:36 PM7/20/18
to mongodb-user
Thanks, Andrew.  That was pretty easy to do and gave me the info I wanted (mostly).  Valgrind also provided info, but quite a bit less clearly in this case.

This is code that works on my staging host but fails when connecting to the same db as staging but from my development machine through an ssh tunnel.  So I'm kind of expecting SSL to cause a failure due to connecting via localhost through a tunnel.  But I'm not expecting a crash.

I'll describe what I found.  Maybe I made a mistake and the input is so malformed that a crash is reasonable.

I connect to mongo thus:

    ostringstream uri_str;
    uri_str << "mongodb://" << mongo_spec.db_username << ":"
            << mongo_spec.db_password << "@" << mongo_spec.db_hostname << ":"
            << mongo_spec.db_port << "/" << mongo_spec.db_name << "?ssl=true";
    LOG_INFO << uri_str.str();
    mongocxx::uri uri(uri_str.str());

    mongocxx::options::client client_options;
    mongocxx::options::ssl ssl_options;
    ssl_options.allow_invalid_certificates(true);
    client_options.ssl_opts(ssl_options);

    try {
        client = mongocxx::client(uri, client_options);
    } catch (const mongocxx::exception& e) {
        LOG_ERROR << "Mongo exception: " << e.what();
        std::rethrow_exception(std::current_exception());
    } catch (...) {
        LOG_ERROR << "WTF?";  // Shouldn't happen.
    }
    return client[mongo_spec.db_name];


I crash in stl_list.h, but really it's here in client.hpp because mongoc_opts.second._M_data = 0.

    client::client(const class uri& uri, const options::client& options) {
    #if defined(MONGOCXX_ENABLE_SSL) && defined(MONGOC_ENABLE_SSL)
        if (options.ssl_opts()) {
            if (!uri.ssl())
                throw exception{error_code::k_invalid_parameter,
                                "cannot set SSL options if 'ssl=true' not in URI"};

            auto mongoc_opts = options::make_ssl_opts(*options.ssl_opts());
    =>      _impl->ssl_options = std::move(mongoc_opts.second);
            libmongoc::client_set_ssl_opts(_get_impl().client_t, &mongoc_opts.first);
        }
    #else
        if (uri.ssl() || options.ssl_opts())
            throw exception{error_code::k_ssl_not_supported};
    #endif
        auto new_client = libmongoc::client_new_from_uri(uri._impl->uri_t);
        if (!new_client) {
            // Shouldn't happen after checks above, but future libmongoc's may change behavior.             
            throw exception{error_code::k_invalid_parameter, "could not construct client from URI"};
        }

        _impl = stdx::make_unique<impl>(std::move(new_client));
    }

If the error is mine, I'll be happy to know it.  If not, I'm happy to file a bug report.

Many thanks!

Jeff Abrahamson

Andrew Morrow

unread,
Jul 20, 2018, 1:16:18 PM7/20/18
to mongod...@googlegroups.com

Can you give me some details on what versions of the C and C++ driver you are using, the platform and toolchain, and how you built them? That will help with debugging.

Jeff Abrahamson

unread,
Jul 20, 2018, 2:13:11 PM7/20/18
to mongodb-user
For builds (c and , I used

cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install


For the rest, I'll let the machine speak for itself:

vagrant@ubuntu-xenial:~$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

vagrant@ubuntu-xenial:~$ uname -a
Linux ubuntu-xenial 4.4.0-130-generic #156-Ubuntu SMP Thu Jun 14 08:53:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
vagrant@ubuntu-xenial:~$ cat /etc/issue
Ubuntu 16.04.4 LTS \n \l

vagrant@ubuntu-xenial:~$ cd src/mongo-c-driver/
vagrant@ubuntu-xenial:~/src/mongo-c-driver$ git log | head -1
commit 7fc3903846fd110e823d971c5f96a148fd3cdf1c
vagrant@ubuntu-xenial:~/src/mongo-c-driver$ cd ../mongo-cxx-driver/
vagrant@ubuntu-xenial:~/src/mongo-cxx-driver$ git log | head -1
commit 9bdb4829ed05612b5fa6386f323a8650b4f342eb
vagrant@ubuntu-xenial:~/src/mongo-cxx-driver$


Reply all
Reply to author
Forward
0 new messages