如何用 swig/cgo 调试 "exit status 3221225477"?

How to debug "exit status 3221225477" with swig/cgo?

我正在使用 SWIG 与 golang 的集成来生成 cgo 文件。我正在链接 C++ 库和 Go。

我已经安装了我的包:swig -go -cgo -c++ -intgosize 64 ./basic_host.igo install 并且一切顺利。 但是当我在这里启动我的主要示例时:

package main

import (
    "fmt"
    "myLib/basic_host"
)

func main() {
    fmt.Println("Hello")
    basic_host.PrintHelpAndExit("test", 0)
    fmt.Println("World!")
}

当我尝试启动它时,我得到 exit status 3221225477 并且我的 print hello 甚至没有被考虑在内,似乎我的程序甚至没有启动......我不知道如何处理这个问题,因为甚至还没有启动调试工具。

退出状态似乎与 NIL 指针引用有关:see here,肯定是由于 cgo 集成。 我正在使用 MinGW 在 Windows 10 上构建,在我的配置下:

swig -version
SWIG Version 4.0.2
Compiled with i686-w64-mingw32-g++ [i686-w64-mingw32]
Configured options: +pcre

我的SWIG接口文件是这样的(basic_host.i):

%module basic_host
%{
    #include <atomic>
    #include <map>
    #include <iostream>
    #include <memory>
    
    #include <myLib/myLib.hpp>
    #include "../../examples/basic_host/command_line.h"
    #include "../../examples/basic_host/command_line.cpp"
%}

%inline %{
    extern void printHelpAndExit(const char* binary, unsigned int exitCode);
%}

这里是生成的go文件(basic_host.go):

    /* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 4.0.2
 *
 * This file is not intended to be easily readable and contains a number of
 * coding conventions designed to improve portability and efficiency. Do not make
 * changes to this file unless you know what you are doing--modify the SWIG
 * interface file instead.
 * ----------------------------------------------------------------------------- */

// source: .\basic_host.i

package basic_host

/*
#define intgo swig_intgo
typedef void *swig_voidp;

#include <stdint.h>


typedef long long intgo;
typedef unsigned long long uintgo;



typedef struct { char *p; intgo n; } _gostring_;
typedef struct { void* array; intgo len; intgo cap; } _goslice_;


typedef _gostring_ swig_type_1;
extern void _wrap_Swig_free_basic_host_5ddda8bb50543998(uintptr_t arg1);
extern uintptr_t _wrap_Swig_malloc_basic_host_5ddda8bb50543998(swig_intgo arg1);
extern void _wrap_printHelpAndExit_basic_host_5ddda8bb50543998(swig_type_1 arg1, swig_intgo arg2);
#undef intgo
*/
import "C"

import "unsafe"
import _ "runtime/cgo"
import "sync"


type _ unsafe.Pointer



var Swig_escape_always_false bool
var Swig_escape_val interface{}


type _swig_fnptr *byte
type _swig_memberptr *byte


type _ sync.Mutex

func Swig_free(arg1 uintptr) {
    _swig_i_0 := arg1
    C._wrap_Swig_free_basic_host_5ddda8bb50543998(C.uintptr_t(_swig_i_0))
}

func Swig_malloc(arg1 int) (_swig_ret uintptr) {
    var swig_r uintptr
    _swig_i_0 := arg1
    swig_r = (uintptr)(C._wrap_Swig_malloc_basic_host_5ddda8bb50543998(C.swig_intgo(_swig_i_0)))
    return swig_r
}

func PrintHelpAndExit(arg1 string, arg2 uint) {
    _swig_i_0 := arg1
    _swig_i_1 := arg2
    C._wrap_printHelpAndExit_basic_host_5ddda8bb50543998(*(*C.swig_type_1)(unsafe.Pointer(&_swig_i_0)), C.swig_intgo(_swig_i_1))
    if Swig_escape_always_false {
        Swig_escape_val = arg1
    }
}

这是 c++ 包装文件 (basic_host_wrap.cxx):

    /* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 4.0.2
 *
 * This file is not intended to be easily readable and contains a number of
 * coding conventions designed to improve portability and efficiency. Do not make
 * changes to this file unless you know what you are doing--modify the SWIG
 * interface file instead.
 * ----------------------------------------------------------------------------- */

// source: .\basic_host.i

#define SWIGMODULE basic_host

#ifdef __cplusplus
/* SwigValueWrapper is described in swig.swg */
template<typename T> class SwigValueWrapper {
  struct SwigMovePointer {
    T *ptr;
    SwigMovePointer(T *p) : ptr(p) { }
    ~SwigMovePointer() { delete ptr; }
    SwigMovePointer& operator=(SwigMovePointer& rhs) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = rhs.ptr; rhs.ptr = 0; return *this; }
  } pointer;
  SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
  SwigValueWrapper(const SwigValueWrapper<T>& rhs);
public:
  SwigValueWrapper() : pointer(0) { }
  SwigValueWrapper& operator=(const T& t) { SwigMovePointer tmp(new T(t)); pointer = tmp; return *this; }
  operator T&() const { return *pointer.ptr; }
  T *operator&() { return pointer.ptr; }
};

template <typename T> T SwigValueInit() {
  return T();
}
#endif

/* -----------------------------------------------------------------------------
 *  This section contains generic SWIG labels for method/variable
 *  declarations/attributes, and other compiler dependent labels.
 * ----------------------------------------------------------------------------- */

/* template workaround for compilers that cannot correctly implement the C++ standard */
#ifndef SWIGTEMPLATEDISAMBIGUATOR
# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560)
#  define SWIGTEMPLATEDISAMBIGUATOR template
# elif defined(__HP_aCC)
/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */
/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */
#  define SWIGTEMPLATEDISAMBIGUATOR template
# else
#  define SWIGTEMPLATEDISAMBIGUATOR
# endif
#endif

/* inline attribute */
#ifndef SWIGINLINE
# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
#   define SWIGINLINE inline
# else
#   define SWIGINLINE
# endif
#endif

/* attribute recognised by some compilers to avoid 'unused' warnings */
#ifndef SWIGUNUSED
# if defined(__GNUC__)
#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
#     define SWIGUNUSED __attribute__ ((__unused__))
#   else
#     define SWIGUNUSED
#   endif
# elif defined(__ICC)
#   define SWIGUNUSED __attribute__ ((__unused__))
# else
#   define SWIGUNUSED
# endif
#endif

#ifndef SWIG_MSC_UNSUPPRESS_4505
# if defined(_MSC_VER)
#   pragma warning(disable : 4505) /* unreferenced local function has been removed */
# endif
#endif

#ifndef SWIGUNUSEDPARM
# ifdef __cplusplus
#   define SWIGUNUSEDPARM(p)
# else
#   define SWIGUNUSEDPARM(p) p SWIGUNUSED
# endif
#endif

/* internal SWIG method */
#ifndef SWIGINTERN
# define SWIGINTERN static SWIGUNUSED
#endif

/* internal inline SWIG method */
#ifndef SWIGINTERNINLINE
# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE
#endif

/* exporting methods */
#if defined(__GNUC__)
#  if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#    ifndef GCC_HASCLASSVISIBILITY
#      define GCC_HASCLASSVISIBILITY
#    endif
#  endif
#endif

#ifndef SWIGEXPORT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#   if defined(STATIC_LINKED)
#     define SWIGEXPORT
#   else
#     define SWIGEXPORT __declspec(dllexport)
#   endif
# else
#   if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
#     define SWIGEXPORT __attribute__ ((visibility("default")))
#   else
#     define SWIGEXPORT
#   endif
# endif
#endif

/* calling conventions for Windows */
#ifndef SWIGSTDCALL
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#   define SWIGSTDCALL __stdcall
# else
#   define SWIGSTDCALL
# endif
#endif

/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
# define _CRT_SECURE_NO_DEPRECATE
#endif

/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
# define _SCL_SECURE_NO_DEPRECATE
#endif

/* Deal with Apple's deprecated 'AssertMacros.h' from Carbon-framework */
#if defined(__APPLE__) && !defined(__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES)
# define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
#endif

/* Intel's compiler complains if a variable which was never initialised is
 * cast to void, which is a common idiom which we use to indicate that we
 * are aware a variable isn't used.  So we just silence that warning.
 * See: https://github.com/swig/swig/issues/192 for more discussion.
 */
#ifdef __INTEL_COMPILER
# pragma warning disable 592
#endif


#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>



typedef long long intgo;
typedef unsigned long long uintgo;


# if !defined(__clang__) && (defined(__i386__) || defined(__x86_64__))
#   define SWIGSTRUCTPACKED __attribute__((__packed__, __gcc_struct__))
# else
#   define SWIGSTRUCTPACKED __attribute__((__packed__))
# endif



typedef struct { char *p; intgo n; } _gostring_;
typedef struct { void* array; intgo len; intgo cap; } _goslice_;




#define swiggo_size_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];
#define swiggo_size_assert(t, n) swiggo_size_assert_eq(sizeof(t), n, swiggo_sizeof_##t##_is_not_##n)

swiggo_size_assert(char, 1)
swiggo_size_assert(short, 2)
swiggo_size_assert(int, 4)
typedef long long swiggo_long_long;
swiggo_size_assert(swiggo_long_long, 8)
swiggo_size_assert(float, 4)
swiggo_size_assert(double, 8)

#ifdef __cplusplus
extern "C" {
#endif
extern void crosscall2(void (*fn)(void *, int), void *, int);
extern char* _cgo_topofstack(void) __attribute__ ((weak));
extern void _cgo_allocate(void *, int);
extern void _cgo_panic(void *, int);
#ifdef __cplusplus
}
#endif

static char *_swig_topofstack() {
  if (_cgo_topofstack) {
    return _cgo_topofstack();
  } else {
    return 0;
  }
}

static void _swig_gopanic(const char *p) {
  struct {
    const char *p;
  } SWIGSTRUCTPACKED a;
  a.p = p;
  crosscall2(_cgo_panic, &a, (int) sizeof a);
}




#define SWIG_contract_assert(expr, msg) \
  if (!(expr)) { _swig_gopanic(msg); } else


static void Swig_free(void* p) {
  free(p);
}

static void* Swig_malloc(int c) {
  return malloc(c);
}


    #include <atomic>
    #include <map>
    #include <iostream>
    #include <memory>
    
    #include <myLib/myLib.hpp>
    #include "../../examples/basic_host/command_line.h"
    #include "../../examples/basic_host/command_line.cpp"


    extern void printHelpAndExit(const char* binary, unsigned int exitCode);

#ifdef __cplusplus
extern "C" {
#endif

void _wrap_Swig_free_basic_host_5ddda8bb50543998(void *_swig_go_0) {
  void *arg1 = (void *) 0 ;
  
  arg1 = *(void **)&_swig_go_0; 
  
  Swig_free(arg1);
  
}


void *_wrap_Swig_malloc_basic_host_5ddda8bb50543998(intgo _swig_go_0) {
  int arg1 ;
  void *result = 0 ;
  void *_swig_go_result;
  
  arg1 = (int)_swig_go_0; 
  
  result = (void *)Swig_malloc(arg1);
  *(void **)&_swig_go_result = (void *)result; 
  return _swig_go_result;
}


void _wrap_printHelpAndExit_basic_host_5ddda8bb50543998(_gostring_ _swig_go_0, intgo _swig_go_1) {
  char *arg1 = (char *) 0 ;
  unsigned int arg2 ;
  
  
  arg1 = (char *)malloc(_swig_go_0.n + 1);
  memcpy(arg1, _swig_go_0.p, _swig_go_0.n);
  arg1[_swig_go_0.n] = '[=16=]';
  
  arg2 = (unsigned int)_swig_go_1; 
  
  printHelpAndExit((char const *)arg1,arg2);
  
  free(arg1); 
}


#ifdef __cplusplus
}
#endif

CPP PrintHelpAndExit中的函数:

void printHelpAndExit(const char* binary, unsigned int exitCode)
{
    std::cout << "Usage: " << binary
            << " -t test " << std::endl;
    std::cout << std::endl;
    std::cout << " -h                          Prints this help and exit" << std::endl;
    std::cout << " -t test                    test desc "
            "desc."
            << std::endl;
    exit(exitCode);
}

如果您对构建问题有任何想法或要调试可能导致此问题的原因。我会很高兴听到它。

更新#1:

我发现如果删除库 <iostream>,我就可以毫无问题地访问 "../../examples/basic_host/command_line.h" 的变量。我仍然不知道为什么这个库特别会导致问题,但我正在调查它。顺便说一句,我不能在我的 C++ 库中删除它,因为我需要它。

我发现另一个人用 python 和 having the same issue as I with iostream but not expalining how to do it 构建 C++ :

更新#2:

似乎它来自我的编译器,我目前正在使用 TDM GCC 并在 windows 下出现错误。我尝试使用 WSL2 Ubuntu 进行编译并且它有效。但是我仍然需要在 Windows 下构建,因为我在我的库中使用 winsock。

我遇到的问题是我在 Windows 上使用的编译器。我以前用 TDM-GCC but there are know issues between this and iostreamlib as this post suggests...

编译

我刚刚将用于使用 SWIG 构建的编译器(gcc、g++、..)更改为 MinGW64:download link。之后一切正常,我可以毫无问题地使用我的库。 感谢那些试图帮助我的人。