Click here to Skip to main content
13,451,369 members (49,363 online)
Click here to Skip to main content


121 bookmarked
Posted 19 Jan 2002

Several classes for exception handling

, 19 Jan 2002
C++ wrappers for stack trace, unhandled exception and win32 structured exceptions
 Copyright (c) 2001 - 2002
 Author: Konstantin Boukreev 
 Created: 25.12.2001 19:41:00
 Version: 1.0.2

 Permission to use, copy, modify, distribute and sell this software
 and its documentation for any purpose is hereby granted without fee,
 provided that the above copyright notice appear in all copies and
 that both that copyright notice and this permission notice appear
 in supporting documentation.  Konstantin Boukreev makes no representations
 about the suitability of this software for any purpose.
 It is provided "as is" without express or implied warranty.
 sym_engine class incapsulate Symbol Handler and Debugging Service API
 the list of used API:
	SymInitialize, SymCleanup, SymGetLineFromAddr, SymGetModuleBase,
	SymGetSymbolInfo, SymGetSymFromAddr, SymGetSymFromName, SymGetSymNext,
	SymLoadModule, SymSetOptions

 based on code\ideas from John Robbins's book "Debugging application"

#ifndef _sym_engine_e4b31bc5_8e01_4cda_b5a4_905dde52ac01
#define _sym_engine_e4b31bc5_8e01_4cda_b5a4_905dde52ac01

#if _MSC_VER > 1000 
#pragma once
#endif // _MSC_VER > 1000

#ifdef _WIN64
#error Win64 is not supported

#include <imagehlp.h>
#include <ostream>

class sym_engine
	sym_engine (unsigned);

	void address(unsigned a)		{ m_address = a; }
	unsigned address(void) const	{ return m_address; }
	// symbol handler queries
	unsigned module  (char *, unsigned);
	unsigned symbol  (char *, unsigned, unsigned * = 0);
	unsigned fileline(char *, unsigned, unsigned *, unsigned * = 0);

	// stack walk
	bool stack_first (CONTEXT* pctx);
	bool stack_next  ();

		format argument
		%m  : module
		%f  : file
		%l  : line
		%ld : line's displacement
		%s  : symbol
		%sd : symbol's displacement
		for example	
		"%f(%l) : %m at %s\n"
		"%m.%s + %sd bytes, in %f:line %l\n"
	static bool stack_trace(std::ostream&, CONTEXT *, unsigned skip = 0, const char * fmt = default_fmt());
	static bool stack_trace(std::ostream&, unsigned skip = 1, const char * fmt = default_fmt()); 	
	static bool stack_trace(std::ostream&, sym_engine&, CONTEXT *,
			unsigned skip = 1, const char * fmt = default_fmt());	
	static const char * default_fmt() { return "%f(%l) : %m at %s\n"; }	
	static bool get_line_from_addr (HANDLE, unsigned, unsigned *, IMAGEHLP_LINE *);
	static unsigned get_module_basename (HMODULE, char *, unsigned);
	bool check();

	unsigned		m_address;
	bool			m_ok;
	STACKFRAME *	m_pframe;
	CONTEXT *		m_pctx;
	class guard
		bool init();		
		bool fail() const { return m_ref == -1; }
		static guard & instance() 
			static guard g; 
			return g;
		void clear();
		bool load_module(HANDLE, HMODULE);
		int  m_ref;

// Change Log:
//		25.12.2001  Konstantin, Initial version.
//		11.01.2002	Konstantin, added skip arg to stack_trace()
//		11.01.2002	Konstantin, format argument to stack_trace()
//		14.01.2002	Konstantin, fixed bug in thread's wait logic
//		16.01.2002, Konstantin, fixed bug in stack_trace(std::ostream&, CONTEXT *, const char*) with skip value
//		16.01.2002, Konstantin, made guard as singelton

#endif //_sym_engine_e4b31bc5_8e01_4cda_b5a4_905dde52ac01

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Konstantin Boukreev
Web Developer
Russian Federation Russian Federation
I am freelance programmer. About 3 years of experience in C++ and I would rather use ATL, STL, WTL but not MFC Smile | :) . Main backgrounds are Win32 API, COM and Networking. Now I am interested about AI (Neural Network, Fuzzy Logic and GA). Currently based in Vladivostok, Russia.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180318.3 | Last Updated 20 Jan 2002
Article Copyright 2002 by Konstantin Boukreev
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid