From 9df281c89586b64dc87f5ae40c7839eb38b65e90 Mon Sep 17 00:00:00 2001 From: Peter Gadfort Date: Wed, 18 Dec 2024 20:50:09 -0700 Subject: [PATCH 1/6] ord: allow exception to be traced via set_debug_level ORD trace 1 Signed-off-by: Peter Gadfort --- src/Exception.i | 18 ++++++++++++++++++ src/cmake/swig_lib.cmake | 8 +++++++- src/odb/src/swig/tcl/CMakeLists.txt | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Exception.i b/src/Exception.i index 91899547d9e..056b70469c6 100644 --- a/src/Exception.i +++ b/src/Exception.i @@ -16,6 +16,14 @@ %{ #include + +#ifndef TEST_BUILD +#include +#include + +#include "ord/OpenRoad.hh" +#include "utl/Logger.h" +#endif %} %exception { @@ -26,6 +34,16 @@ } // This catches std::runtime_error (utl::error) and sta::Exception. catch (std::exception &excp) { +%#ifndef TEST_BUILD + auto logger = ord::OpenRoad::openRoad()->getLogger(); + if (logger->debugCheck(utl::ORD, "trace", 1)) { + std::stringstream trace; + trace << boost::stacktrace::stacktrace(); + logger->report("Stack trace"); + logger->report(trace.str()); + } +%#endif + Tcl_ResetResult(interp); Tcl_AppendResult(interp, excp.what(), nullptr); return TCL_ERROR; diff --git a/src/cmake/swig_lib.cmake b/src/cmake/swig_lib.cmake index fa93b3fa217..e50f0fb4522 100644 --- a/src/cmake/swig_lib.cmake +++ b/src/cmake/swig_lib.cmake @@ -15,7 +15,7 @@ function(swig_lib) # Parse args set(options "") set(oneValueArgs I_FILE NAME NAMESPACE LANGUAGE RUNTIME_HEADER) - set(multiValueArgs SWIG_INCLUDES SCRIPTS) + set(multiValueArgs SWIG_INCLUDES SCRIPTS DEFINES) cmake_parse_arguments( ARG # prefix on the parsed args @@ -130,6 +130,12 @@ function(swig_lib) ) endif() + if (DEFINED ARG_DEFINES) + target_compile_definitions(${ARG_NAME} + PRIVATE ${ARG_DEFINES} + ) + endif() + # Generate the encoded of the script files. if (DEFINED ARG_SCRIPTS) set(LANG_INIT ${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}-${ARG_LANGUAGE}InitVar.cc) diff --git a/src/odb/src/swig/tcl/CMakeLists.txt b/src/odb/src/swig/tcl/CMakeLists.txt index 46eed4301b9..5110e8c6342 100644 --- a/src/odb/src/swig/tcl/CMakeLists.txt +++ b/src/odb/src/swig/tcl/CMakeLists.txt @@ -5,6 +5,7 @@ swig_lib(NAME odbtcl I_FILE ../common/odb.i SWIG_INCLUDES ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/src/swig/tcl + DEFINES TEST_BUILD ) target_include_directories(odbtcl From 477798b4c5b79d21e1849a82ac23a2f1a721b573 Mon Sep 17 00:00:00 2001 From: Peter Gadfort Date: Wed, 18 Dec 2024 20:50:50 -0700 Subject: [PATCH 2/6] ord: use abort instead of exit to allow for stacktrace to be printed Signed-off-by: Peter Gadfort --- src/Exception.i | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Exception.i b/src/Exception.i index 056b70469c6..6a9f045a43a 100644 --- a/src/Exception.i +++ b/src/Exception.i @@ -16,6 +16,7 @@ %{ #include +#include #ifndef TEST_BUILD #include @@ -30,7 +31,7 @@ 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) { From 91c6498c135292ea38752eb41286ffb075ee789f Mon Sep 17 00:00:00 2001 From: Peter Gadfort Date: Thu, 19 Dec 2024 09:48:00 -0700 Subject: [PATCH 3/6] ord: remove TEST_BUILD define and replace with correct stub.cpp Signed-off-by: Peter Gadfort --- src/Exception.i | 19 +++++++------ src/cmake/swig_lib.cmake | 8 +----- src/odb/src/swig/tcl/CMakeLists.txt | 4 ++- src/odb/src/swig/tcl/stub.cpp | 42 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 src/odb/src/swig/tcl/stub.cpp diff --git a/src/Exception.i b/src/Exception.i index 6a9f045a43a..ecea128991e 100644 --- a/src/Exception.i +++ b/src/Exception.i @@ -18,13 +18,11 @@ #include #include -#ifndef TEST_BUILD #include #include #include "ord/OpenRoad.hh" #include "utl/Logger.h" -#endif %} %exception { @@ -35,15 +33,16 @@ } // This catches std::runtime_error (utl::error) and sta::Exception. catch (std::exception &excp) { -%#ifndef TEST_BUILD - auto logger = ord::OpenRoad::openRoad()->getLogger(); - if (logger->debugCheck(utl::ORD, "trace", 1)) { - std::stringstream trace; - trace << boost::stacktrace::stacktrace(); - logger->report("Stack trace"); - logger->report(trace.str()); + 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()); + } } -%#endif Tcl_ResetResult(interp); Tcl_AppendResult(interp, excp.what(), nullptr); diff --git a/src/cmake/swig_lib.cmake b/src/cmake/swig_lib.cmake index e50f0fb4522..fa93b3fa217 100644 --- a/src/cmake/swig_lib.cmake +++ b/src/cmake/swig_lib.cmake @@ -15,7 +15,7 @@ function(swig_lib) # Parse args set(options "") set(oneValueArgs I_FILE NAME NAMESPACE LANGUAGE RUNTIME_HEADER) - set(multiValueArgs SWIG_INCLUDES SCRIPTS DEFINES) + set(multiValueArgs SWIG_INCLUDES SCRIPTS) cmake_parse_arguments( ARG # prefix on the parsed args @@ -130,12 +130,6 @@ function(swig_lib) ) endif() - if (DEFINED ARG_DEFINES) - target_compile_definitions(${ARG_NAME} - PRIVATE ${ARG_DEFINES} - ) - endif() - # Generate the encoded of the script files. if (DEFINED ARG_SCRIPTS) set(LANG_INIT ${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}-${ARG_LANGUAGE}InitVar.cc) diff --git a/src/odb/src/swig/tcl/CMakeLists.txt b/src/odb/src/swig/tcl/CMakeLists.txt index 5110e8c6342..65f0722ade8 100644 --- a/src/odb/src/swig/tcl/CMakeLists.txt +++ b/src/odb/src/swig/tcl/CMakeLists.txt @@ -5,7 +5,6 @@ swig_lib(NAME odbtcl I_FILE ../common/odb.i SWIG_INCLUDES ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/src/swig/tcl - DEFINES TEST_BUILD ) target_include_directories(odbtcl @@ -26,11 +25,14 @@ 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 diff --git a/src/odb/src/swig/tcl/stub.cpp b/src/odb/src/swig/tcl/stub.cpp new file mode 100644 index 00000000000..d7780e36037 --- /dev/null +++ b/src/odb/src/swig/tcl/stub.cpp @@ -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 From 2c10b9f98fc4b98f9e29f576b9a02f8895f76f8a Mon Sep 17 00:00:00 2001 From: Peter Gadfort Date: Thu, 19 Dec 2024 10:56:31 -0700 Subject: [PATCH 4/6] ord: add stacktrace to python exception Signed-off-by: Peter Gadfort --- src/Exception-py.i | 31 ++++++++++++++++++++++++++++++- src/par/CMakeLists.txt | 1 + 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Exception-py.i b/src/Exception-py.i index 30686eec216..7d47627e4e4 100644 --- a/src/Exception-py.i +++ b/src/Exception-py.i @@ -32,20 +32,49 @@ /////////////////////////////////////////////////////////////////////////////// %{ +#include + +#include +#include + +#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; } diff --git a/src/par/CMakeLists.txt b/src/par/CMakeLists.txt index da78b853f76..dd274b9f315 100644 --- a/src/par/CMakeLists.txt +++ b/src/par/CMakeLists.txt @@ -154,6 +154,7 @@ if (Python3_FOUND AND BUILD_PYTHON) target_link_libraries(par_py PUBLIC par_lib + utl_lib ) endif() From bb06a3b6fca9adea2122319608749c3d5206bad7 Mon Sep 17 00:00:00 2001 From: Peter Gadfort Date: Thu, 19 Dec 2024 15:05:41 -0700 Subject: [PATCH 5/6] dst: add dl to link Signed-off-by: Peter Gadfort --- src/dst/test/cpp/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dst/test/cpp/CMakeLists.txt b/src/dst/test/cpp/CMakeLists.txt index 0beb1e33b16..fbfe2aeded2 100644 --- a/src/dst/test/cpp/CMakeLists.txt +++ b/src/dst/test/cpp/CMakeLists.txt @@ -1,5 +1,6 @@ set(TEST_LIBS dst + dl ${TCL_LIBRARY} ) From 799c6b34f070496a1a42b1ca4023da11332a9a89 Mon Sep 17 00:00:00 2001 From: Peter Gadfort Date: Thu, 19 Dec 2024 15:46:32 -0700 Subject: [PATCH 6/6] add dl to odbtcl-bin Signed-off-by: Peter Gadfort --- src/odb/src/swig/tcl/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/odb/src/swig/tcl/CMakeLists.txt b/src/odb/src/swig/tcl/CMakeLists.txt index 65f0722ade8..313cdf62251 100644 --- a/src/odb/src/swig/tcl/CMakeLists.txt +++ b/src/odb/src/swig/tcl/CMakeLists.txt @@ -38,6 +38,7 @@ target_include_directories(odbtcl-bin target_link_libraries(odbtcl-bin PUBLIC odbtcl + dl ) set_target_properties(odbtcl-bin