无法导入python模块(雷司令)

时间:2014-04-08 20:55:59

标签: python swig

我正在尝试运行SAM(SPARC架构建模工具),在编译之后,当我使用“run_sam.sh”脚本时,它调用python接口并发出错误:

starting py interface...
UI: start py thread, tid = 5
stop: Traceback (most recent call last):
  File "lib/frontend/sam_n1.py", line 26, in ?
    import riesling_n1
  File "/scratch/sam-t1/lib/frontend/riesling_n1.py", line 4, in ?
    import _riesling_n1
ImportError: dynamic module does not define init function (init_riesling_n1)

附加Makefile.swig:

 # ========== Copyright Header Begin ==========================================
     2  #
     3  # OpenSPARC T1 Processor File: Makefile.swig
     4  # Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
     5  # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
     6  #
     7  # The above named program is free software; you can redistribute it and/or
     8  # modify it under the terms of the GNU General Public
     9  # License version 2 as published by the Free Software Foundation.
    10  #
    11  # The above named program is distributed in the hope that it will be
    12  # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    14  # General Public License for more details.
    15  #
    16  # You should have received a copy of the GNU General Public
    17  # License along with this work; if not, write to the Free Software
    18  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
    19  #
    20  # ========== Copyright Header End ============================================
    21  RIESLING_ROOT           = ../..
    22
    23  include $(RIESLING_ROOT)/src/Makefile.moddefs
    24
    25  MODULE                  = swig
    26  DEPENDENT_MODULES       = $(MODULE) fw mmu asi strand core cpu system trap blaze register
    27
    28  NAMESPACE               = ""
    29
    30  CPPFLAGS                += -I$(DEVTOOLS)/shade/inc -I$(PYTHONINC)
    31
    32  include Makefile.$(PRODUCT)
    33  include $(RIESLING_ROOT)/src/Makefile.modrules
    34
    35  autoregs.i: AutoRegs.xml
    36          PYTHONPATH=$(PYTHONPATH) $(PYTHON) genregs ../../src AutoRegs.xml autoregs.i
    37
    38  riesling_$(PRODUCT)_wrap.cc: riesling_$(PRODUCT).i autoregs.i strand.i system.i fw.i conv.i
    39          $(SWIG) $(SWIG_FLAGS) -o $@ riesling_$(PRODUCT).i
    40
    41  riesling_$(PRODUCT)_blaze_wrap.cc: riesling_$(PRODUCT)_blaze.i riesling_$(PRODUCT).i autoregs.i strand.i system.i fw.i conv.i
    42          $(SWIG) $(SWIG_FLAGS) -o $@ riesling_$(PRODUCT)_blaze.i

我对此非常陌生,如果不能正确提问,请提前道歉。请帮忙。

我看到以下文件:“riesling_n1.i”,“riesling_n1_wrap.cc”和“riesling_n1.py”。我认为最后一个是运行Makefile.swig后创建的python文件。

2014年4月10日:

Schollii,这是Makefile.n1:

    1  # ========== Copyright Header Begin ==========================================
     2  #
     3  # OpenSPARC T1 Processor File: Makefile.n1
     4  # Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
     5  # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
     6  #
     7  # The above named program is free software; you can redistribute it and/or
     8  # modify it under the terms of the GNU General Public
     9  # License version 2 as published by the Free Software Foundation.
    10  #
    11  # The above named program is distributed in the hope that it will be
    12  # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    14  # General Public License for more details.
    15  #
    16  # You should have received a copy of the GNU General Public
    17  # License along with this work; if not, write to the Free Software
    18  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
    19  #
    20  # ========== Copyright Header End ============================================
    21
    22  REGISTERED_XML_FILES.n1 =
    23
    24  XML_FILES.n1 =
    25
    26  REGISTERED_CCFILES.n1 =
    27
    28  CCFILES.n1 =\
    29                  riesling_$(PRODUCT)_wrap.cc
    30
~

此外,riesling_n1.i代码在这里:

/*
    * ========== Copyright Header Begin ==========================================
    * 
    * OpenSPARC T1 Processor File: riesling_n1.i
    * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
    * 
    * The above named program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public
    * License version 2 as published by the Free Software Foundation.
    * 
    * The above named program is distributed in the hope that it will be 
    * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    * General Public License for more details.
    * 
    * You should have received a copy of the GNU General Public
    * License along with this work; if not, write to the Free Software
    * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
    * 
* ========== Copyright Header End ============================================
*/
%module riesling_n1

/*==========================================================================*\
 * NOTE: Some method signatures use uint64_t even though the
 * actual riesling method defines the type as uint32_t.  The
 * 8-byte type is used to keep python from sign extending unsigned
 * 4-byte values when they are converted to a python long types.  
 *==========================================================================*/

%{
typedef uint64_t VaddrT;
typedef uint64_t PaddrT;
typedef uint16_t ContextT;

#include "Ni/Ni_System.h"
//#include "Ni/Ni_CBInterface.h"
#include "Ni/Ni_ArchStateConf.h"
#include "Ni/Ni_Tlb.h"
#include "Ni/Ni_Tte.h"
%}

%ignore operator=;
%ignore operator<<;
/* print is a Python keyword, so we rename it here */
%rename (rsPrint) print;

#define RIESLING_REGISTER_CONSTRUCTOR( class_name, ...  ) class_name ( __VA_ARGS__ )
#define RIESLING_REGISTER_METHOD( handle, return_type, method_name, ...  ) return_type method_name ( __VA_ARGS__ )
#define RIESLING_REGISTER_CHILD( handle, type, var_name )\ type var_name
#define RIESLING_REGISTER_CHILD_PTR( handle, type, var_name )\ type var_name
#define RIESLING_CACHE_RV( handle, cacheName, size )

%include "fw.i"
%include "strand.i"
%include "cpu.i"
%include "system.i"
#include "autoregs.i"
%include "std_string.i"
%include "conv.i"

%inline %{
//   n1_system_base() is used when we already have created a N1_Cpu elsewhere
//   and we have a long value representing its pointer. n1_system_base() casts
//   it to a proper SWIG pointer 

Riesling::Ni_SystemBase* n1_system_base( uint64_t p )
{
  union { Riesling::Ni_SystemBase* sys; uint64_t ptr; } u;
  u.ptr = p;
  return u.sys;
}
%}


namespace Riesling {

/*--------------------------------------------------------------------------*/

/*-------------------------------------
class Ni_PerfCtlReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_PicReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_IsfsrReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_DsfsrReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_DsfarReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*--------------------------------------------------------------------------*/

class Ni_Tlb
{
  public:
    std::string toString();
    Riesling::TtePos insert(const Riesling::Ni_Tte &tte);
    Riesling::Ni_Tte* at(int);
    uint32_t getNLines();
    int next_valid_index(int i);
};

class Ni_Tte
{
public:
    Ni_Tte();
    std::string toString() const;
    Riesling::Ni_TteDataSun4u& data4u();
    void      context( uint16_t c );
    uint16_t  context() const;  
    void   pid( uint32_t p );
    uint32_t  pid() const;
    uint64_t vaddr() const;
    void   vaddr( uint64_t vaddr );
    uint32_t  r() const;
    void   r( uint32_t _r );
    uint64_t translate( VaddrT vaddr ) const;
    bool  tagMatch (  uint64_t va, uint16_t context ) const;
        bool  tagMatch (  uint64_t va, uint16_t context, uint32_t pid, uint32_t r ) const;
};

class Ni_TteDataSun4u
{
public:
    Ni_TteDataSun4u();

    uint64_t getNative() const;
    void setNative( uint64_t value );

    uint64_t getV( ) const;
    uint64_t getSZL( ) const;
    uint64_t getNFO( ) const;
    uint64_t getIE( ) const;
    uint64_t getSZH( ) const;
    uint64_t getDIAG7_3( ) const;
    uint64_t getPA( ) const;
    uint64_t getL( ) const;
    uint64_t getCP( ) const;
    uint64_t getCV( ) const;
    uint64_t getE( ) const;
    uint64_t getP( ) const;
    uint64_t getW( ) const;

    void setV( uint64_t value );
    void setSZL( uint64_t value );
    void setNFO( uint64_t value );
    void setIE( uint64_t value );
    void setSZH( uint64_t value );
    void setDIAG7_3( uint64_t value );
    void setPA( uint64_t value );
    void setL( uint64_t value );
    void setCP( uint64_t value );
    void setCV( uint64_t value );
    void setE( uint64_t value );
    void setP( uint64_t value );
    void setW( uint64_t value );        
};





class Ni_IDPartitionIdReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Sf_ContextReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

/*-------------------------------------
class Ni_TlbDataIn
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_TlbDataAccess
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

class Sf_TagTargetReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Sf_TagAccessReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

/*-------------------------------------
class Ni_TsbSearch
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_RealRange
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_PhysOffset
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_TsbConfig
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_TsbPointer
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_TimedTlb
{
  public:
    std::string toString();
};
-------------------------------------*/

class Ni_Mmu
{
  public:
    std::string toString();
    //int getContextSize();
    //int getTsbConfigSize();
    //Ni_IDPartitionIdReg* getPartitionIdPtr();
    //Sf_ContextReg* getPrimaryContextPtr(int i);
    //Sf_ContextReg* getSecondaryContextPtr(int i);
    //Ni_TlbDataIn* getItlbDataInPtr();
    //Ni_TlbDataAccess* getItlbDataAccessPtr();
    //Sf_TagTargetReg* getItagTargetPtr();
    //Sf_TagAccessReg* getItagAccessPtr();
    //Ni_IsfsrReg* getIsfsrPtr();
    //Ni_TlbDataIn* getDtlbDataInPtr();
    //Ni_TlbDataAccess* getDtlbDataAccessPtr();
    //Sf_TagTargetReg* getDtagTargetPtr();
    //Sf_TagAccessReg* getDtagAccessPtr();
    //Ni_DsfsrReg* getDsfsrPtr();
    //Ni_DsfarReg* getDsfarPtr();
    //Ni_TsbSearch* getTsbSearchPtr();
    //Ni_RealRange* getRealRangePtr(int i);
    //Ni_PhysOffset* getPhysOffsetPtr(int i);
    //Ni_TsbConfig* getZeroContextTsbConfigPtr(int i);
    //Ni_TsbConfig* getNonzeroContextTsbConfigPtr(int i);
    //Ni_TsbPointer* getItsbPointerPtr(int i);
    //Ni_TsbPointer* getDtsbPointerPtr(int i);
};

/*--------------------------------------------------------------------------*/

/*-------------------------------------
class CMP_CoreIdReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};    
-------------------------------------*/

/*-------------------------------------
class CMP_CoreInterruptIdReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*--------------------------------------------------------------------------*/

class Hv_InstructionWord
{
  public:
    std::string toString() const;
    uint32_t getNative() const;
    uint64_t getPc() const;
    uint64_t getPpc() const;
};
class Ni_InstructionWord
{
  public:
    std::string toString() const;
    uint32_t getNative() const;
    uint64_t getPc() const;
    uint64_t getPpc() const;
};

class Ni_TstateEntry
{
  public:
    Ni_TstateEntry();
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Ni_Tstate
{
  public:
    std::string toString();
    Ni_TstateEntry getTstateEntry(uint32_t);
    void           setTstateEntry(uint32_t, const Ni_TstateEntry&);
};

class Ni_PstateReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_HtstateEntry 
{
  public:
    Hv_HtstateEntry();
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_Htstate
{
  public:
    std::string     toString();
    Hv_HtstateEntry getHtstateEntry(uint32_t);
    void            setHtstateEntry(uint32_t, const Hv_HtstateEntry&);
    int         getMaxTl();
};

class Vis2_GsrReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_HpstateReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_HtbaReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Ni_HverReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_HintpReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_HstickCompareReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_GlobalLevelReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Hv_ScratchPad
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};

class Ni_ArchState 
{
  public:

    std::string toString();

    uint64_t getPc();
    void     setPc(uint64_t);
    uint64_t getNpc();
    void     setNpc(uint64_t);

    RegisterFile*        getRegisterFilePtr();
    FloatRegisterFile*   getFloatRegisterFilePtr();
    Ni_Tstate*           getTstateRegPtr();
    Ni_PstateReg*    getPstateRegPtr();
    Hv_Htstate*      getHtstateRegPtr();
    Vis2_GsrReg*         getGsrRegPtr();
    Hv_HpstateReg*   getHpstateRegPtr();
    Hv_HtbaReg*      getHtbaRegPtr();
    Ni_HverReg*      getHverRegPtr();
    //Ni_PerfCtlReg*       getNiPcrRegPtr();
    //Ni_PicReg*           getNiPicRegPtr();
    Hv_HintpReg*     getHintpRegPtr();
    Hv_HstickCompareReg* getHstickCompareRegPtr();
    Hv_GlobalLevelReg*   getGlobalLevelRegPtr();
    Hv_ScratchPad*       getScratchPadPtr(int);
    V9_TickReg*          getStickRegPtr();
    Sf_SoftIntReg*       getSoftIntRegPtr();
    V9_TickCompareReg*   getTickCmprRegPtr();
    V9_TickCompareReg*   getStickCmprRegPtr();
    V9_CcrReg*           getCcrRegPtr();
    V9_YReg*             getYRegPtr();
    V9_FprsReg*          getFprsRegPtr();
    V9_FsrReg*           getFsrRegPtr();
    V9_TickReg*          getTickRegPtr();
    V9_TbaReg*           getTbaRegPtr();
    V9_PilReg*           getPilRegPtr();
    V9_AsiReg*           getAsiRegPtr();
    V9_Tpc*              getTpcRegPtr();
    V9_Tnpc*             getTnpcRegPtr();
    V9_WstateReg*        getWstateRegPtr();
    V9_TrapLevelReg*     getTrapLevelRegPtr();
    V9_VerReg*           getVerRegPtr();
    V9_TrapType*         getTrapTypeRegPtr();
    //CMP_CoreIdReg* getCmpCoreIdRegPtr();
    //CMP_CoreInterruptIdReg* getCmpCoreIntrIdRegPtr();
};

/*-------------------------------------
class Ni_WatchPointReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

/*-------------------------------------
class Ni_LsuControlReg
{
  public:
    std::string toString();
    uint64_t    getNative();
    void        setNative(uint64_t);
};
-------------------------------------*/

class Ni_Strand 
{
  public:
    %extend {
    uint_t get_max_tl()     { return Riesling::Ni_ArchStateConf::MAXTL; }
    uint_t get_max_ptl()    { return Riesling::Ni_ArchStateConf::MAXPTL; }
    uint_t get_max_gl()     { return Riesling::Ni_ArchStateConf::MAXGL; }
    uint_t get_max_pgl()    { return Riesling::Ni_ArchStateConf::MAXGL; } // used in done/retry instruction ... 
    }
    int         step();
    Ni_ArchState*   getArchStatePtr();
    Ni_InstructionWord* getLastInstrPtr();
    Ni_Mmu*         getMmuPtr();
    //Ni_WatchPointReg*     getWatchpointPtr();
    //Ni_LsuControlReg*     getLsuControlPtr();
    uint32_t        lastInstr();
    std::string     lastInstrToString();

    /* when a (uint32_t)iw has bit31=1, the python->C++ conversion complains,
       so have to use uint64_t here to fool it, what a pain */
    Ni_InstructionWord* RS_getInstrPtr(uint64_t);
    uint64_t        RS_translate(int, uint64_t);
    uint64_t        RS_access(uint64_t, uint64_t, int, int);
    uint64_t            RS_asiRead (int asi, uint64_t va, bool nse);
    void                RS_asiWrite(int asi, uint64_t va, uint64_t value, bool nse);
    std::string         RS_dumpTlb(int itlb, int valid);
};

/*--------------------------------------------------------------------------*/

class Ni_Core
{
  public:
    std::string        toString();
    uint_t             getNStrands() const;
    Ni_Strand*         getStrandPtr(uint_t);
    Ni_Tlb*     getdTlbPtr();
    Ni_Tlb*        getiTlbPtr();
};

/*--------------------------------------------------------------------------*/

class Ni_Cpu 
{
  public:
    std::string          toString();
    uint_t               getNCores() const;
    Ni_Core*             getCorePtr(uint_t);
    //CMP_CpuLevelCmpRegs* getCpuLevelCmpRegsPtr();
};



/*--------------------------------------------------------------------------*/

class Ni_System 
{
  public:
    Ni_System();
    std::string   toString();
    uint_t        getNCpus() const;
    Ni_Cpu*       getCpuPtr(uint_t);
    void          loadMemdatImage(const std::string&);
    SparseMemory* getRam();
    uint64_t      getThisPtr();
    BreakpointTable*    getBreakpointTablePtr();
};

/*--------------------------------------------------------------------------*/

class Ni_SystemBase 
{
  public:
    Ni_SystemBase();
    uint_t        getNCpus() const;
    Ni_Cpu*       getCpuPtr(uint_t);
    BreakpointTable*    getBreakpointTablePtr();
};

/*-------------------------------------
class Ni_CBInterface
{   
  public:   
    static void suspendCB();
    static void resumeCB();
};
-------------------------------------*/

}; /* namespace Riesling */

我在Makefile中找到了以下几行代码和其他代码:

@/bin/cp -fp ../$(LIB_PATH)/riesling_n1.py $(INSTALL)/frontend/.
    @/bin/cp -fp ../$(LIB_PATH)/libriesling_n1_vcpu.so $(INSTALL)/.
    @/bin/cp -fp ../$(LIB_PATH)/libriesling_n1_vcpu.so $(INSTALL)/libSUNW,UltraSPARC-T1.so
    @/bin/cp -fp ../$(LIB_PATH)/_riesling_n1.so $(INSTALL)/.

而且,一旦我编译swig并因此得到原始错误,_riesling_n1.so就会被破坏。当我用原始的(与包附带的那个)替换_riesling_n1.so时,SAM工作。这意味着在编译期间它会以某种方式弄乱共享对象库文件。

请告知(如果可能的话)找出造成这种情况的原因。

1 个答案:

答案 0 :(得分:1)

根据SWIG 2.0文档:

  

当给共享对象文件指定错误名称时,几乎总是会导致此错误。例如,如果您创建了一个文件example.so而不是_example.so,则会出现此错误。或者,如果模块的名称与%module指令提供的模块名称不一致,则可能会出现此错误。仔细检查接口以确保模块名称和共享对象文件名匹配。导致此错误的另一个可能原因是在创建扩展模块时忘记将SWIG生成的包装器代码与应用程序的其余部分链接。

因此检查_riesling_n1.so是否存在且可由动态加载程序找到(即它位于启动py接口的文件夹中或LD_LIBRARY_PATH中)。我没有在你发布的代码中看到任何.so make规则,所以也许.so甚至没有构建(.py文件的存在与此不矛盾;该文件由SWIG创建)。同时确保%module中的riesling_n1.iriesling_n1