Changeset 90aeb9


Ignore:
Timestamp:
Aug 4, 2010, 2:17:49 PM (14 years ago)
Author:
Tillmann Crueger <crueger@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
00fca3
Parents:
796aa6
Message:

Added a method to access the backtrace when an assert fails

Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Helpers/Assert.cpp

    r796aa6 r90aeb9  
    4646#ifndef NDEBUG
    4747
     48#ifdef __GNUC__
     49#include <cstdlib>
     50#include <execinfo.h>
     51#include <cxxabi.h>
     52#endif
     53
    4854Assert::Action Assert::_my_assert::defaultAction = Ask;
    4955std::vector<Assert::hook_t> Assert::_my_assert::hooks;
     
    5258const char* Assert::_wrapper::message_ptr = "source pointer did not point to object of desired type";
    5359const char* Assert::_wrapper::message_ref = "source reference did not contain object of desired type";
    54 
    5560
    5661bool Assert::_my_assert::check(const bool res,
     
    6772      char choice;
    6873      if(defaultAction==Assert::Ask) {
     74#ifdef __GNUC__
     75        cout << "Please choose: (a)bort, (t)hrow execption, show (b)actrace, (i)gnore, al(w)ays ignore" << endl;
     76#else
    6977        cout << "Please choose: (a)bort, (t)hrow execption, (i)gnore, al(w)ays ignore" << endl;
     78#endif /* __GNUC__ */
    7079        cin >> choice;
    7180      }
     
    8089          throw AssertionFailure(condition,filename,line,message);
    8190          break;
     91#ifdef __GNUC__
     92        case 'b':
     93          Assert::_my_assert::backtrace(filename,line);
     94         break;
     95#endif /* __GNUC__ */
    8296        case 'w':
    8397          ignore = true;
     
    92106}
    93107
     108#ifdef __GNUC__
     109void Assert::_my_assert::backtrace(const char *file, int line){
     110  const size_t max_depth = 100;
     111  void* stack_addrs[max_depth];
     112  size_t stack_depth;
     113  char **stack_strings=0;
     114  const char *func_name=0;
     115  size_t sz = 64;
     116
     117  // get the backtrace
     118  stack_depth   = ::backtrace(stack_addrs,max_depth);
     119  stack_strings = backtrace_symbols(stack_addrs, stack_depth);
     120  // used later for demangling
     121  // reserved here, so we can free it unconditionally
     122  char *dm_function = static_cast<char*>(malloc(sz));
     123  if(!dm_function){
     124    // malloc failed... we are out of luck
     125    cout << "cannot provide stack trace due to exhausted memory" << endl;
     126    return;
     127  }
     128
     129  cout << "Backtrace from  " << file << "@" << line << ":" << endl;
     130
     131  // i=2 because we don't want this function, nor the assertion handler
     132  for(unsigned int i=2;i<stack_depth-2;++i){
     133    // find the mangled function name
     134    char *begin = stack_strings[i];
     135    // function name starts with a (
     136    while(*begin && *begin!='(') ++begin;
     137    char *end=begin;
     138    while(*end && *end!='+') ++end;
     139
     140    // see if we found our function name
     141    if(*begin && *end){
     142      *begin++ = 0;
     143      *end = 0;
     144      // use the C++ demangler
     145
     146      int status;
     147      char *func_ret = abi::__cxa_demangle(begin, dm_function, &sz, &status);
     148      if(func_ret){
     149        // abi might have realloced...
     150        dm_function = func_ret;
     151        func_name = dm_function;
     152      }
     153      else{
     154        // demangling failed... get the function name without demangling
     155        func_name = begin;
     156      }
     157    }
     158    else{
     159      // function name not found... get the whole line
     160      func_name = stack_strings[i];
     161    }
     162    cout << func_name << endl;
     163  }
     164  free(dm_function);
     165  free(stack_strings); // malloc()ed by backtrace_symbols
     166}
     167#endif /* __GNUC__ */
     168
    94169void Assert::_my_assert::doHooks(){
    95170  for(vector<hook_t>::reverse_iterator iter = hooks.rbegin(); iter!=hooks.rend(); ++iter ){
  • src/Helpers/Assert.hpp

    r796aa6 r90aeb9  
    305305                        const int line,
    306306                        bool& ignore);
     307#ifdef __GNUC__
     308      static void backtrace(const char *file, int line);
     309#endif /* __GNUC__ */
    307310      static void addHook(Assert::hook_t hook);
    308311      static void removeHook(Assert::hook_t hook);
  • src/UIElements/TextUI/TextWindow.cpp

    r796aa6 r90aeb9  
    2525#include "Views/StreamStringView.hpp"
    2626#include "Views/MethodStringView.hpp"
    27 #include "Helpers/MemDebug.hpp"
     27#include "Helpers/Assert.hpp"
    2828
    2929#include "defs.hpp"
     
    4444TextWindow::TextWindow()
    4545{
     46  ASSERT(0,"just some test");
    4647  map <std::string, TextMenu *> NametoTextMenuMap;
    4748
Note: See TracChangeset for help on using the changeset viewer.