LCOV - code coverage report
Current view: top level - bridges/source/cpp_uno/gcc3_linux_intel - except.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 85 102 83.3 %
Date: 2014-04-11 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <cstdio>
      21             : #include <cstring>
      22             : #include <dlfcn.h>
      23             : #include <boost/unordered_map.hpp>
      24             : 
      25             : #include <rtl/strbuf.hxx>
      26             : #include <rtl/ustrbuf.hxx>
      27             : #include <osl/diagnose.h>
      28             : #include <osl/mutex.hxx>
      29             : 
      30             : #include <com/sun/star/uno/genfunc.hxx>
      31             : #include "com/sun/star/uno/RuntimeException.hpp"
      32             : #include <typelib/typedescription.hxx>
      33             : 
      34             : #include "share.hxx"
      35             : 
      36             : using namespace ::std;
      37             : using namespace ::osl;
      38             : using namespace ::rtl;
      39             : using namespace ::com::sun::star::uno;
      40             : using namespace ::__cxxabiv1;
      41             : 
      42             : 
      43             : namespace CPPU_CURRENT_NAMESPACE
      44             : {
      45             : 
      46           0 : void dummy_can_throw_anything( char const * )
      47             : {
      48           0 : }
      49             : 
      50       23227 : static OUString toUNOname( char const * p ) SAL_THROW(())
      51             : {
      52             : #if OSL_DEBUG_LEVEL > 1
      53             :     char const * start = p;
      54             : #endif
      55             : 
      56             :     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
      57             : 
      58       23227 :     OUStringBuffer buf( 64 );
      59             :     OSL_ASSERT( 'N' == *p );
      60       23227 :     ++p; // skip N
      61             : 
      62      163621 :     while ('E' != *p)
      63             :     {
      64             :         // read chars count
      65      117167 :         long n = (*p++ - '0');
      66      257564 :         while ('0' <= *p && '9' >= *p)
      67             :         {
      68       23230 :             n *= 10;
      69       23230 :             n += (*p++ - '0');
      70             :         }
      71      117167 :         buf.appendAscii( p, n );
      72      117167 :         p += n;
      73      117167 :         if ('E' != *p)
      74       93940 :             buf.append( '.' );
      75             :     }
      76             : 
      77             : #if OSL_DEBUG_LEVEL > 1
      78             :     OUString ret( buf.makeStringAndClear() );
      79             :     OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
      80             :     fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
      81             :     return ret;
      82             : #else
      83       23227 :     return buf.makeStringAndClear();
      84             : #endif
      85             : }
      86             : 
      87             : class RTTI
      88             : {
      89             :     typedef boost::unordered_map< OUString, type_info *, OUStringHash > t_rtti_map;
      90             : 
      91             :     Mutex m_mutex;
      92             :     t_rtti_map m_rttis;
      93             :     t_rtti_map m_generatedRttis;
      94             : 
      95             :     void * m_hApp;
      96             : 
      97             : public:
      98             :     RTTI() SAL_THROW(());
      99             :     ~RTTI() SAL_THROW(());
     100             : 
     101             :     type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW(());
     102             : };
     103             : 
     104         131 : RTTI::RTTI() SAL_THROW(())
     105             : #if defined(FREEBSD) && __FreeBSD_version < 702104
     106             :     : m_hApp( dlopen( 0, RTLD_NOW | RTLD_GLOBAL ) )
     107             : #else
     108         131 :     : m_hApp( dlopen( 0, RTLD_LAZY ) )
     109             : #endif
     110             : {
     111         131 : }
     112             : 
     113         262 : RTTI::~RTTI() SAL_THROW(())
     114             : {
     115         131 :     dlclose( m_hApp );
     116         131 : }
     117             : 
     118             : 
     119       19823 : type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW(())
     120             : {
     121             :     type_info * rtti;
     122             : 
     123       19823 :     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
     124             : 
     125       19823 :     MutexGuard guard( m_mutex );
     126       19823 :     t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
     127       19823 :     if (iRttiFind == m_rttis.end())
     128             :     {
     129             :         // RTTI symbol
     130         174 :         OStringBuffer buf( 64 );
     131         174 :         buf.append( "_ZTIN" );
     132         174 :         sal_Int32 index = 0;
     133         870 :         do
     134             :         {
     135         870 :             OUString token( unoName.getToken( 0, '.', index ) );
     136         870 :             buf.append( token.getLength() );
     137        1740 :             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
     138        1740 :             buf.append( c_token );
     139             :         }
     140         870 :         while (index >= 0);
     141         174 :         buf.append( 'E' );
     142             : 
     143         348 :         OString symName( buf.makeStringAndClear() );
     144             : #if defined(FREEBSD) && __FreeBSD_version < 702104 /* #i22253# */
     145             :         rtti = (type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
     146             : #else
     147         174 :         rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
     148             : #endif
     149             : 
     150         174 :         if (rtti)
     151             :         {
     152             :             pair< t_rtti_map::iterator, bool > insertion(
     153         172 :                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
     154             :             SAL_WARN_IF( !insertion.second, "bridges", "### inserting new rtti failed?!" );
     155             :         }
     156             :         else
     157             :         {
     158             :             // try to lookup the symbol in the generated rtti map
     159           2 :             t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
     160           2 :             if (iFind == m_generatedRttis.end())
     161             :             {
     162             :                 // we must generate it !
     163             :                 // symbol and rtti-name is nearly identical,
     164             :                 // the symbol is prefixed with _ZTI
     165           2 :                 char const * rttiName = symName.getStr() +4;
     166             : #if OSL_DEBUG_LEVEL > 1
     167             :                 fprintf( stderr,"generated rtti for %s\n", rttiName );
     168             : #endif
     169           2 :                 if (pTypeDescr->pBaseTypeDescription)
     170             :                 {
     171             :                     // ensure availability of base
     172             :                     type_info * base_rtti = getRTTI(
     173           2 :                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
     174             :                     rtti = new __si_class_type_info(
     175           2 :                         strdup( rttiName ), (__class_type_info *)base_rtti );
     176             :                 }
     177             :                 else
     178             :                 {
     179             :                     // this class has no base class
     180           0 :                     rtti = new __class_type_info( strdup( rttiName ) );
     181             :                 }
     182             : 
     183             :                 pair< t_rtti_map::iterator, bool > insertion(
     184           2 :                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
     185             :                 SAL_WARN_IF( !insertion.second, "bridges", "### inserting new generated rtti failed?!" );
     186             :             }
     187             :             else // taking already generated rtti
     188             :             {
     189           0 :                 rtti = iFind->second;
     190             :             }
     191         174 :         }
     192             :     }
     193             :     else
     194             :     {
     195       19649 :         rtti = iRttiFind->second;
     196             :     }
     197             : 
     198       19823 :     return rtti;
     199             : }
     200             : 
     201             : 
     202             : extern "C" {
     203       19821 : static void _GLIBCXX_CDTOR_CALLABI deleteException( void * pExc )
     204             : {
     205       19821 :     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
     206       19821 :     typelib_TypeDescription * pTD = 0;
     207       19821 :     OUString unoName( toUNOname( header->exceptionType->name() ) );
     208       19821 :     ::typelib_typedescription_getByName( &pTD, unoName.pData );
     209             :     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
     210       19821 :     if (pTD)
     211             :     {
     212       19821 :         ::uno_destructData( pExc, pTD, cpp_release );
     213       19821 :         ::typelib_typedescription_release( pTD );
     214       19821 :     }
     215       19821 : }
     216             : }
     217             : 
     218       19821 : void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
     219             : {
     220             : #if OSL_DEBUG_LEVEL > 1
     221             :     OString cstr(
     222             :         OUStringToOString(
     223             :             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
     224             :             RTL_TEXTENCODING_ASCII_US ) );
     225             :     fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
     226             : #endif
     227             :     void * pCppExc;
     228             :     type_info * rtti;
     229             : 
     230             :     {
     231             :     // construct cpp exception object
     232       19821 :     typelib_TypeDescription * pTypeDescr = 0;
     233       19821 :     TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
     234             :     OSL_ASSERT( pTypeDescr );
     235       19821 :     if (! pTypeDescr)
     236             :     {
     237             :         throw RuntimeException(
     238           0 :             OUString("cannot get typedescription for type ") +
     239           0 :             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
     240           0 :             Reference< XInterface >() );
     241             :     }
     242             : 
     243       19821 :     pCppExc = __cxxabiv1::__cxa_allocate_exception( pTypeDescr->nSize );
     244       19821 :     ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
     245             : 
     246             :     // destruct uno exception
     247       19821 :     ::uno_any_destruct( pUnoExc, 0 );
     248             :     // avoiding locked counts
     249             :     static RTTI * s_rtti = 0;
     250       19821 :     if (! s_rtti)
     251             :     {
     252         131 :         MutexGuard guard( Mutex::getGlobalMutex() );
     253         131 :         if (! s_rtti)
     254             :         {
     255             : #ifdef LEAK_STATIC_DATA
     256             :             s_rtti = new RTTI();
     257             : #else
     258         131 :             static RTTI rtti_data;
     259         131 :             s_rtti = &rtti_data;
     260             : #endif
     261         131 :         }
     262             :     }
     263       19821 :     rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
     264       19821 :     TYPELIB_DANGER_RELEASE( pTypeDescr );
     265             :     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
     266       19821 :     if (! rtti)
     267             :     {
     268             :         throw RuntimeException(
     269           0 :             OUString("no rtti for type ") +
     270           0 :             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
     271           0 :             Reference< XInterface >() );
     272             :     }
     273             :     }
     274             : 
     275       19821 :     __cxxabiv1::__cxa_throw( pCppExc, rtti, deleteException );
     276             : }
     277             : 
     278        3406 : void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
     279             : {
     280        3406 :     if (! header)
     281             :     {
     282             :         RuntimeException aRE(
     283             :             OUString("no exception header!"),
     284           0 :             Reference< XInterface >() );
     285           0 :         Type const & rType = ::getCppuType( &aRE );
     286           0 :         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
     287             : #if OSL_DEBUG_LEVEL > 0
     288             :         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
     289             :         OSL_FAIL( cstr.getStr() );
     290             : #endif
     291        3406 :         return;
     292             :     }
     293             : 
     294        3406 :     typelib_TypeDescription * pExcTypeDescr = 0;
     295        3406 :     OUString unoName( toUNOname( header->exceptionType->name() ) );
     296             : #if OSL_DEBUG_LEVEL > 1
     297             :     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
     298             :     fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
     299             : #endif
     300        3406 :     typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
     301        3406 :     if (0 == pExcTypeDescr)
     302             :     {
     303             :         RuntimeException aRE(
     304           0 :             OUString("exception type not found: ") + unoName,
     305           0 :             Reference< XInterface >() );
     306           0 :         Type const & rType = ::getCppuType( &aRE );
     307           0 :         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
     308             : #if OSL_DEBUG_LEVEL > 0
     309             :         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
     310             :         OSL_FAIL( cstr.getStr() );
     311             : #endif
     312             :     }
     313             :     else
     314             :     {
     315             :         // construct uno exception any
     316        3406 :         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
     317        3406 :         typelib_typedescription_release( pExcTypeDescr );
     318        3406 :     }
     319             : }
     320             : 
     321             : }
     322             : 
     323             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10