1

I have the following proto:

syntax = "proto3";
package pb;

message FooBar {
  string foo = 1;
  string bar = 2;
}

message FooBarList {
  repeated FooBar foobar = 1;
}

I just populate the repeated list with 2 values and try to print it as bellow:

#include <iostream>
#include "foobar.pb.h"

int main()
{
    pb::FooBarList foobar_list;

    auto foobar1 = foobar_list.add_foobar();
    foobar1->set_foo("foo1");
    foobar1->set_bar("bar1");

    auto foobar2 = foobar_list.add_foobar();
    foobar2->set_foo("foo2");
    foobar2->set_bar("bar2");

    try {
        std::cout << foobar_list.ShortDebugString() << std::endl;
    }
    catch (const std::exception& e) {
        std::cout << "error: " << e.what() << std::endl;
        return -1;
    }

    return 0;
}

This generates the following output:

error: Unknown error -1

So, ShortDebugString() triggers an exception down the line which does not have any meaningful information.
Am I doing something wrong, or do I need somehow to trigger the serialization before calling ShortDebugString()?

Here is the gdb callstack if I let the exeption propagate:

Program received signal SIGABRT, Aborted.
0x00007ffff7add355 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7add355 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7ac6853 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7e5d86a in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7e69d8a in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:48
#4  0x00007ffff7e69df7 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:58
#5  0x00007ffff7e6a09e in __cxxabiv1::__cxa_throw (obj=obj@entry=0x5555556e1550, 
    tinfo=tinfo@entry=0x7ffff7f97590 <typeinfo for std::system_error>, dest=dest@entry=0x7ffff7e96860 <std::system_error::~system_error()>)
    at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#6  0x00007ffff7e609bc in std::__throw_system_error (__i=-1)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/new_allocator.h:89
#7  0x00005555555a1018 in google::protobuf::internal::AssignDescriptors(google::protobuf::internal::DescriptorTable const*, bool) ()
#8  0x000055555556e77f in pb::FooBarList::GetMetadataStatic() ()
#9  0x000055555556ceb9 in pb::FooBarList::GetMetadata() const ()
#10 0x00005555555d1fb4 in google::protobuf::TextFormat::Printer::Print(google::protobuf::Message const&, google::protobuf::TextFormat::Printer::TextGenerator*) const ()
#11 0x00005555555d3ef9 in google::protobuf::Message::ShortDebugString[abi:cxx11]() const ()
#12 0x00005555555705ac in main ()
(gdb) 

EDIT

Here is the CMakeLists.txt used to build this project:

cmake_minimum_required(VERSION 3.10)
project(proto-foobar)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(Protobuf_USE_STATIC_LIBS ON)
find_package(Protobuf MODULE REQUIRED)

set(PROTO_DEFINITIONS_PATH "${CMAKE_SOURCE_DIR}/protos")
set(PROTO_DEFINITIONS "${PROTO_DEFINITIONS_PATH}/*.proto")
set(PROTO_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/source")
file(MAKE_DIRECTORY ${PROTO_OUTPUT_PATH})

set(PROTO_HEADERS "${PROTO_OUTPUT_PATH}/foobar.pb.h")
set(PROTO_SOURCES "${PROTO_OUTPUT_PATH}/foobar.pb.cc")

add_custom_command(OUTPUT ${PROTO_HEADERS} ${PROTO_SOURCES}
                   COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
                   ARGS --cpp_out=${PROTO_OUTPUT_PATH} 
                        --proto_path=${PROTO_DEFINITIONS_PATH} 
                        ${PROTO_DEFINITIONS}
                   DEPENDS ${PROTO_DEFINITIONS}
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                   COMMENT "Compiling ${PROJECT_NAME} protos")    

file(GLOB_RECURSE PROJECT_FILES "${CMAKE_SOURCE_DIR}/source/*.cpp")
add_executable(${PROJECT_NAME} ${PROTO_SOURCES} ${PROJECT_FILES})

target_link_libraries(${PROJECT_NAME}       PUBLIC ${PROTOBUF_LIBRARY})
target_include_directories(${PROJECT_NAME}  PUBLIC ${PROTO_OUTPUT_PATH})

Here is the project tree:

├── CMakeLists.txt
├── protos
│   └── foobar.proto
└── source
    ├── foobar.pb.cc
    ├── foobar.pb.h
    └── main.cpp
codentary
  • 993
  • 1
  • 14
  • 33
  • Working fine. Output: `foobar { foo: "foo1" bar: "bar1" } foobar { foo: "foo2" bar: "bar2" }`. How did you compile? Command? – Azeem Jun 06 '20 at 05:58
  • @Azeem, I added more details to the question. – codentary Jun 08 '20 at 12:19
  • Have you tested it other than CMake? – Azeem Jun 08 '20 at 16:52
  • @Azeem, This example project in particular, no. The thing is that I use kind of the same `cmake` in some other project and it works fine. I did this example initially to showcase some other issue, but then I got this when I was trying to print for debugging. – codentary Jun 11 '20 at 02:47
  • Right. You can test this without cmake to see if that works. There might be some configuration issue, in this case. Otherwise, the issue might be of the runtime. – Azeem Jun 11 '20 at 03:07

1 Answers1

2

(Had the exact same issue and dug into it a bit.)

The trigger in this case is this piece of code. It contains (indirectly) a call to std::call_once, which on Linux is implemented using pthread_once. That, in turn, requires that your binary is linked against the pthread library (i.e. it requires an -lpthread linker flag).

If your binary has not been linked against pthread you get the error you saw, as mentioned here along with a recipe for fixing this.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • Thanks! In addition to the `-lpthread`, I also needed the `-pthread` flag to fix my crash. More details also here: https://github.com/protocolbuffers/protobuf/issues/4958 (Note: I was using protobuf 3.9.2 and gcc 9.3.0) – arr_sea Sep 11 '20 at 17:34