Skip to content

Commit

Permalink
Merge pull request The-OpenROAD-Project#6384 from gadfort/exception-t…
Browse files Browse the repository at this point in the history
…race

ord: add option to print stacktrace on exception
  • Loading branch information
gadfort authored Dec 20, 2024
2 parents fa2632f + 799c6b3 commit 5f20e59
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/Exception-py.i
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,49 @@
///////////////////////////////////////////////////////////////////////////////

%{
#include <stdlib.h>

#include <boost/stacktrace.hpp>
#include <sstream>

#include "ord/OpenRoad.hh"
#include "utl/Logger.h"
%}

%exception {
try { $function }
catch (std::bad_alloc &) {
fprintf(stderr, "Error: out of memory.");
exit(0);
abort();
}
// This catches std::runtime_error (utl::error) and sta::Exception.
catch (std::exception &excp) {
auto* openroad = ord::OpenRoad::openRoad();
if (openroad != nullptr) {
auto* logger = openroad->getLogger();
if (logger->debugCheck(utl::ORD, "trace", 1)) {
std::stringstream trace;
trace << boost::stacktrace::stacktrace();
logger->report("Stack trace");
logger->report(trace.str());
}
}

PyErr_SetString(PyExc_RuntimeError, excp.what());
SWIG_fail;
}
catch (...) {
auto* openroad = ord::OpenRoad::openRoad();
if (openroad != nullptr) {
auto* logger = openroad->getLogger();
if (logger->debugCheck(utl::ORD, "trace", 1)) {
std::stringstream trace;
trace << boost::stacktrace::stacktrace();
logger->report("Stack trace");
logger->report(trace.str());
}
}

PyErr_SetString(PyExc_Exception, "Unknown exception");
SWIG_fail;
}
Expand Down
20 changes: 19 additions & 1 deletion src/Exception.i
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,34 @@

%{
#include <new>
#include <stdlib.h>

#include <boost/stacktrace.hpp>
#include <sstream>

#include "ord/OpenRoad.hh"
#include "utl/Logger.h"
%}

%exception {
try { $function }
catch (std::bad_alloc &) {
fprintf(stderr, "Error: out of memory.");
exit(1);
abort();
}
// This catches std::runtime_error (utl::error) and sta::Exception.
catch (std::exception &excp) {
auto* openroad = ord::OpenRoad::openRoad();
if (openroad != nullptr) {
auto* logger = openroad->getLogger();
if (logger->debugCheck(utl::ORD, "trace", 1)) {
std::stringstream trace;
trace << boost::stacktrace::stacktrace();
logger->report("Stack trace");
logger->report(trace.str());
}
}

Tcl_ResetResult(interp);
Tcl_AppendResult(interp, excp.what(), nullptr);
return TCL_ERROR;
Expand Down
1 change: 1 addition & 0 deletions src/dst/test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(TEST_LIBS
dst
dl
${TCL_LIBRARY}
)

Expand Down
4 changes: 4 additions & 0 deletions src/odb/src/swig/tcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ target_link_libraries(odbtcl
# Executable
add_executable(odbtcl-bin
main.cpp
stub.cpp
)

target_include_directories(odbtcl-bin
PUBLIC
${PROJECT_SOURCE_DIR}/include/odb
PRIVATE
${OPENROAD_HOME}/include
)

target_link_libraries(odbtcl-bin
PUBLIC
odbtcl
dl
)

set_target_properties(odbtcl-bin
Expand Down
42 changes: 42 additions & 0 deletions src/odb/src/swig/tcl/stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
///////////////////////////////////////////////////////////////////////////////
// BSD 3-Clause License
//
// Copyright (c) 2024, The Regents of the University of California
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include "ord/OpenRoad.hh"

namespace ord {

OpenRoad* OpenRoad::openRoad()
{
return nullptr;
}

} // namespace ord
1 change: 1 addition & 0 deletions src/par/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ if (Python3_FOUND AND BUILD_PYTHON)
target_link_libraries(par_py
PUBLIC
par_lib
utl_lib
)

endif()
Expand Down

0 comments on commit 5f20e59

Please sign in to comment.