1 | // |
---|
2 | // Debugger.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Debugger.cpp#4 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: Debugger |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/Debugger.h" |
---|
38 | #include <sstream> |
---|
39 | #include <cstdlib> |
---|
40 | #include <cstdio> |
---|
41 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
42 | #include "Poco/UnWindows.h" |
---|
43 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
44 | #include <unistd.h> |
---|
45 | #include <signal.h> |
---|
46 | #elif defined(POCO_OS_FAMILY_VMS) |
---|
47 | #include <lib$routines.h> |
---|
48 | #include <ssdef.h> |
---|
49 | #endif |
---|
50 | #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING) |
---|
51 | #include "Poco/UnicodeConverter.h" |
---|
52 | #endif |
---|
53 | |
---|
54 | |
---|
55 | // NOTE: In this module, we use the C library functions (fputs) for, |
---|
56 | // output since, at the time we're called, the C++ iostream objects std::cout, etc. |
---|
57 | // might not have been initialized yet. |
---|
58 | |
---|
59 | |
---|
60 | namespace Poco { |
---|
61 | |
---|
62 | |
---|
63 | bool Debugger::isAvailable() |
---|
64 | { |
---|
65 | #if defined(_DEBUG) |
---|
66 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
67 | return IsDebuggerPresent() ? true : false; |
---|
68 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
69 | return std::getenv("POCO_ENABLE_DEBUGGER") ? true : false; |
---|
70 | #elif defined(POCO_OS_FAMILY_VMS) |
---|
71 | return true; |
---|
72 | #endif |
---|
73 | #else |
---|
74 | return false; |
---|
75 | #endif |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void Debugger::message(const std::string& msg) |
---|
80 | { |
---|
81 | #if defined(_DEBUG) |
---|
82 | std::fputs("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n", stderr); |
---|
83 | std::fputs(msg.c_str(), stderr); |
---|
84 | std::fputs("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n", stderr); |
---|
85 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
86 | if (IsDebuggerPresent()) |
---|
87 | { |
---|
88 | #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING) |
---|
89 | std::wstring umsg; |
---|
90 | UnicodeConverter::toUTF16(msg, umsg); |
---|
91 | umsg += '\n'; |
---|
92 | OutputDebugStringW(umsg.c_str()); |
---|
93 | #else |
---|
94 | OutputDebugStringA(msg.c_str()); |
---|
95 | OutputDebugStringA("\n"); |
---|
96 | #endif |
---|
97 | } |
---|
98 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
99 | #elif defined(POCO_OS_FAMILY_VMS) |
---|
100 | #endif |
---|
101 | #endif |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | void Debugger::message(const std::string& msg, const char* file, int line) |
---|
106 | { |
---|
107 | #if defined(_DEBUG) |
---|
108 | std::ostringstream str; |
---|
109 | str << msg << " [in file \"" << file << "\", line " << line << "]"; |
---|
110 | message(str.str()); |
---|
111 | #endif |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void Debugger::enter() |
---|
116 | { |
---|
117 | #if defined(_DEBUG) |
---|
118 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
119 | if (IsDebuggerPresent()) |
---|
120 | { |
---|
121 | DebugBreak(); |
---|
122 | } |
---|
123 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
124 | if (isAvailable()) |
---|
125 | { |
---|
126 | kill(getpid(), SIGINT); |
---|
127 | } |
---|
128 | #elif defined(POCO_OS_FAMILY_VMS) |
---|
129 | { |
---|
130 | const char* cmd = "\012SHOW CALLS"; |
---|
131 | lib$signal(SS$_DEBUG, 1, cmd); |
---|
132 | } |
---|
133 | #endif |
---|
134 | #endif |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | void Debugger::enter(const std::string& msg) |
---|
139 | { |
---|
140 | #if defined(_DEBUG) |
---|
141 | message(msg); |
---|
142 | enter(); |
---|
143 | #endif |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | void Debugger::enter(const std::string& msg, const char* file, int line) |
---|
148 | { |
---|
149 | #if defined(_DEBUG) |
---|
150 | message(msg, file, line); |
---|
151 | enter(); |
---|
152 | #endif |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | void Debugger::enter(const char* file, int line) |
---|
157 | { |
---|
158 | #if defined(_DEBUG) |
---|
159 | message("BREAK", file, line); |
---|
160 | enter(); |
---|
161 | #endif |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | } // namespace Poco |
---|