Merge lp:~zorba-coders/zorba/debugger_ctrl_c into lp:zorba

Proposed by Gabriel Petrovay
Status: Superseded
Proposed branch: lp:~zorba-coders/zorba/debugger_ctrl_c
Merge into: lp:zorba
Diff against target: 559 lines (+263/-68)
11 files modified
bin/CMakeLists.txt (+1/-0)
bin/debugger/command_prompt.cpp (+12/-1)
bin/debugger/main.cpp (+48/-55)
bin/debugger/xqdb_client.cpp (+63/-0)
bin/debugger/xqdb_client.h (+51/-0)
src/debugger/debugger_clientimpl.cpp (+8/-3)
src/debugger/debugger_commons.cpp (+7/-1)
src/debugger/debugger_runtime.cpp (+18/-3)
src/debugger/debugger_runtime.h (+8/-4)
src/debugger/debugger_server.cpp (+38/-1)
src/debugger/debugger_server.h (+9/-0)
To merge this branch: bzr merge lp:~zorba-coders/zorba/debugger_ctrl_c
Reviewer Review Type Date Requested Status
David Graf (community) Needs Fixing
Gabriel Petrovay (community) Approve
Review via email: mp+88918@code.launchpad.net

Commit message

Added Ctrl-C hooking in the debugger. Ctrl-C can not kill the debugger anymore. It will only stop the running query.

Description of the change

Added Ctrl-C hooking in the debugger. Ctrl-C can not kill the debugger anymore. It will only stop the running query.

To post a comment you must log in.
Revision history for this message
Gabriel Petrovay (gabipetrovay) :
review: Approve
Revision history for this message
David Graf (davidagraf) wrote :

I tested it. Works really nice. I just have some little comments concerning the code:

1)
Do we already have a bug entry for this:
336 // TODO: this was the initial implementation. This will have to change
337 - this->sleep_(1000);
338 + this->sleep_(250);

2)
Do these lines of code block the default behavior of ctrl-C? If yes, can you please at a comment?
134 +#ifdef WIN32
135 +BOOL WINAPI
136 +ctrlC_Handler(DWORD aCtrlType)
137 +{
138 + if (CTRL_C_EVENT == aCtrlType) {
139 + return true;
140 + }
141 + return false;
142 +}
143 +#else
144 +void
145 +ctrlC_Handler(int lParam)
146 +{
147 + //exit(1);
148 +}
149 +#endif
150 +

3)
Strange variable name: theNotBremse :-)

review: Needs Fixing
10621. By Gabriel Petrovay

comment renamings in code

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/CMakeLists.txt'
2--- bin/CMakeLists.txt 2012-01-11 17:30:25 +0000
3+++ bin/CMakeLists.txt 2012-01-18 12:03:24 +0000
4@@ -31,6 +31,7 @@
5
6 SET (DEBUG_CLIENT_SRCS
7 debugger/main.cpp
8+ debugger/xqdb_client.cpp
9 debugger/process_listener.cpp
10 debugger/command_prompt.cpp
11 debugger/command_line_handler.cpp
12
13=== modified file 'bin/debugger/command_prompt.cpp'
14--- bin/debugger/command_prompt.cpp 2012-01-11 17:30:25 +0000
15+++ bin/debugger/command_prompt.cpp 2012-01-18 12:03:24 +0000
16@@ -94,6 +94,8 @@
17 void
18 CommandPrompt::execute()
19 {
20+ bool lWithOutput = true;
21+
22 for (;;) {
23 #ifdef ZORBA_HAVE_LIBEDIT_H
24 const char* lBuf;
25@@ -101,10 +103,19 @@
26 lBuf = el_gets(theEditLine, &lCharsRead);
27 std::string lCommandLine(lBuf, lCharsRead - 1);
28 #else
29- std::cout << "(xqdb) ";
30+ if (lWithOutput) {
31+ std::cout << "(xqdb) ";
32+ }
33+ lWithOutput = true;
34 std::string lCommandLine;
35 std::getline(std::cin, lCommandLine);
36+ if (std::cin.fail()) {
37+ lWithOutput = false;
38+ std::cin.clear();
39+ continue;
40+ }
41 #endif
42+
43 std::vector<std::string> lArgs;
44
45 // split the command into arguments
46
47=== modified file 'bin/debugger/main.cpp'
48--- bin/debugger/main.cpp 2012-01-11 17:30:25 +0000
49+++ bin/debugger/main.cpp 2012-01-18 12:03:24 +0000
50@@ -24,67 +24,47 @@
51
52 #include <zorba/config.h>
53
54-#include "command_prompt.h"
55-#include "command_line_handler.h"
56+#include "xqdb_client.h"
57 #include "process_listener.h"
58
59+
60 using namespace zorba;
61 using namespace zorba::debugger;
62
63-class XqdbClient {
64-
65- public:
66-
67- XqdbClient(unsigned int aPort)
68- {
69- theIdQueue = new LockFreeQueue<std::size_t>();
70- theQuitQueue = new LockFreeQueue<bool>();
71- theEventHandler = new EventHandler(*theIdQueue, *theQuitQueue);
72- theEventHandler->init();
73-
74- theCommandPrompt = new CommandPrompt();
75- theCommandLineHandler = new CommandLineHandler(aPort, *theIdQueue, *theQuitQueue, theEventHandler, theCommandPrompt);
76- }
77-
78- ~XqdbClient()
79- {
80- if (theCommandLineHandler) {
81- delete theCommandLineHandler;
82- }
83- if (theCommandPrompt) {
84- delete theCommandPrompt;
85- }
86- if (theEventHandler) {
87- delete theEventHandler;
88- }
89-
90- delete theIdQueue;
91- delete theQuitQueue;
92- }
93-
94- void start()
95- {
96- theCommandLineHandler->execute();
97- }
98-
99- private:
100-
101- LockFreeQueue<std::size_t>* theIdQueue;
102- LockFreeQueue<bool>* theQuitQueue;
103-
104- EventHandler* theEventHandler;
105- CommandPrompt* theCommandPrompt;
106- CommandLineHandler* theCommandLineHandler;
107-};
108-
109-
110+
111+std::auto_ptr<XqdbClient> theClient;
112+
113+// this will make sure the xqdb process will not quit when Ctrl-C is pressed
114+#ifdef WIN32
115+BOOL WINAPI
116+ctrlC_Handler(DWORD aCtrlType)
117+{
118+ if (CTRL_C_EVENT == aCtrlType) {
119+ return true;
120+ }
121+ return false;
122+}
123+#else
124+void
125+ctrlC_Handler(int lParam)
126+{
127+ // an empty sugnal handler on Linux should do the job
128+}
129+#endif
130+
131+
132+// this handler function is passed the the zorba process listener and will
133+// the client if the zorba process terminates
134 void
135 onExitProcess(ExitCode aExitCode) {
136- //if (aExitCode != -1) {
137- // std::cout << "Zorba has exited with code: " << aExitCode << std::endl;
138- //}
139- std::cout << "Terminating debugger client."<< std::endl;
140- // TODO: and the memory?
141+ std::cout << std::endl << "Terminating debugger client." << std::endl;
142+
143+#ifndef WIN32
144+ XqdbClient* lClient = theClient.release();
145+ if (lClient) {
146+ delete lClient;
147+ }
148+#endif
149
150 exit(aExitCode);
151 }
152@@ -322,6 +302,12 @@
153 _tmain(int argc, _TCHAR* argv[])
154 #endif
155 {
156+#ifdef WIN32
157+ SetConsoleCtrlHandler(ctrlC_Handler, TRUE);
158+#else
159+ signal(SIGINT, ctrlC_Handler);
160+#endif
161+
162 // **************************************************************************
163 // processing arguments
164
165@@ -369,12 +355,19 @@
166 // **************************************************************************
167 // start the debugger command line
168
169- std::auto_ptr<XqdbClient> theClient(new XqdbClient(lPort));
170+ theClient.reset(new XqdbClient(lPort));
171 theClient->start();
172
173 } catch (...) {
174 return -1;
175 }
176
177+#ifndef WIN32
178+ XqdbClient* lClient = theClient.release();
179+ if (lClient) {
180+ delete lClient;
181+ }
182+#endif
183+
184 return 0;
185 }
186
187=== added file 'bin/debugger/xqdb_client.cpp'
188--- bin/debugger/xqdb_client.cpp 1970-01-01 00:00:00 +0000
189+++ bin/debugger/xqdb_client.cpp 2012-01-18 12:03:24 +0000
190@@ -0,0 +1,63 @@
191+/*
192+ * Copyright 2006-2008 The FLWOR Foundation.
193+ *
194+ * Licensed under the Apache License, Version 2.0 (the "License");
195+ * you may not use this file except in compliance with the License.
196+ * You may obtain a copy of the License at
197+ *
198+ * http://www.apache.org/licenses/LICENSE-2.0
199+ *
200+ * Unless required by applicable law or agreed to in writing, software
201+ * distributed under the License is distributed on an "AS IS" BASIS,
202+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203+ * See the License for the specific language governing permissions and
204+ * limitations under the License.
205+ */
206+
207+#include "xqdb_client.h"
208+
209+#include <iostream>
210+
211+#ifdef ZORBA_HAVE_PTHREAD_H
212+# include <cassert>
213+#endif
214+
215+
216+namespace zorba { namespace debugger {
217+
218+XqdbClient::XqdbClient(unsigned int aPort)
219+{
220+ theIdQueue = new LockFreeQueue<std::size_t>();
221+ theQuitQueue = new LockFreeQueue<bool>();
222+ theEventHandler = new EventHandler(*theIdQueue, *theQuitQueue);
223+ theEventHandler->init();
224+
225+ theCommandPrompt = new CommandPrompt();
226+ theCommandLineHandler = new CommandLineHandler(aPort, *theIdQueue, *theQuitQueue, theEventHandler, theCommandPrompt);
227+}
228+
229+XqdbClient::~XqdbClient()
230+{
231+ if (theCommandLineHandler) {
232+ delete theCommandLineHandler;
233+ }
234+ if (theCommandPrompt) {
235+ delete theCommandPrompt;
236+ }
237+ if (theEventHandler) {
238+ delete theEventHandler;
239+ }
240+
241+ delete theIdQueue;
242+ delete theQuitQueue;
243+}
244+
245+void
246+XqdbClient::start()
247+{
248+ theCommandLineHandler->execute();
249+}
250+
251+
252+} // namespace zorba
253+} // namespace debugger
254
255=== added file 'bin/debugger/xqdb_client.h'
256--- bin/debugger/xqdb_client.h 1970-01-01 00:00:00 +0000
257+++ bin/debugger/xqdb_client.h 2012-01-18 12:03:24 +0000
258@@ -0,0 +1,51 @@
259+/*
260+ * Copyright 2006-2008 The FLWOR Foundation.
261+ *
262+ * Licensed under the Apache License, Version 2.0 (the "License");
263+ * you may not use this file except in compliance with the License.
264+ * You may obtain a copy of the License at
265+ *
266+ * http://www.apache.org/licenses/LICENSE-2.0
267+ *
268+ * Unless required by applicable law or agreed to in writing, software
269+ * distributed under the License is distributed on an "AS IS" BASIS,
270+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
271+ * See the License for the specific language governing permissions and
272+ * limitations under the License.
273+ */
274+#pragma once
275+#ifndef ZORBA_DEBUGGER_XQDB_CLIENT_H
276+#define ZORBA_DEBUGGER_XQDB_CLIENT_H
277+
278+#include "command_prompt.h"
279+#include "command_line_handler.h"
280+
281+
282+namespace zorba { namespace debugger {
283+
284+class XqdbClient {
285+
286+ public:
287+
288+ XqdbClient(unsigned int aPort);
289+
290+ ~XqdbClient();
291+
292+ void
293+ start();
294+
295+ private:
296+
297+ LockFreeQueue<std::size_t>* theIdQueue;
298+ LockFreeQueue<bool>* theQuitQueue;
299+
300+ EventHandler* theEventHandler;
301+ CommandPrompt* theCommandPrompt;
302+ CommandLineHandler* theCommandLineHandler;
303+};
304+
305+
306+} // namespace zorba
307+} // namespace debugger
308+
309+#endif // ZORBA_DEBUGGER_XQDB_CLIENT_H
310
311=== modified file 'src/debugger/debugger_clientimpl.cpp'
312--- src/debugger/debugger_clientimpl.cpp 2012-01-11 17:30:25 +0000
313+++ src/debugger/debugger_clientimpl.cpp 2012-01-18 12:03:24 +0000
314@@ -53,17 +53,22 @@
315 lStr >> length;
316 #endif
317
318- // we are not interested in the length, but only in the init message
319 std::getline(*(theClient->theInStream), str, '\0');
320
321+ // only assert if we have a good stream (no stream was broken,
322+ // case in which an empty string will be returned)
323+ if (theClient->theInStream->good()) {
324 #ifndef NDEBUG
325- assert(str.size() == length);
326+ assert(str.size() == length);
327 #endif
328+ } else {
329+ break;
330+ }
331
332 theClient->theHandler->parseMessage(str);
333
334 // TODO: this was the initial implementation. This will have to change
335- this->sleep_(1000);
336+ this->sleep_(250);
337 }
338 }
339
340
341=== modified file 'src/debugger/debugger_commons.cpp'
342--- src/debugger/debugger_commons.cpp 2012-01-11 17:30:25 +0000
343+++ src/debugger/debugger_commons.cpp 2012-01-18 12:03:24 +0000
344@@ -35,6 +35,8 @@
345
346 #include "zorba/util/uri.h"
347
348+#include "debugger_runtime.h"
349+
350 namespace zorba {
351
352 // ****************************************************************************
353@@ -382,8 +384,12 @@
354 bool
355 DebuggerCommons::mustBreak(SuspensionCause& aCause)
356 {
357+ if (theRuntime->getAndClearInterruptBreak()) {
358+ aCause = CAUSE_USER;
359+ return true;
360+ }
361 if (theBreak) {
362- aCause = CAUSE_STEP;
363+ aCause = theCause;
364 return true;
365 } else if (theStepping) {
366 std::size_t lSize = theIteratorStack.size();
367
368=== modified file 'src/debugger/debugger_runtime.cpp'
369--- src/debugger/debugger_runtime.cpp 2012-01-11 17:30:25 +0000
370+++ src/debugger/debugger_runtime.cpp 2012-01-18 12:03:24 +0000
371@@ -60,7 +60,8 @@
372 Zorba_SerializerOptions& serializerOptions,
373 DebuggerCommunicator* communicator,
374 itemHandler aHandler,
375- void* aCallBackData)
376+ void* aCallBackData,
377+ bool* aInterruptBreak)
378 : theQuery(xqueryImpl),
379 theOStream(oStream),
380 theSerializerOptions(serializerOptions),
381@@ -71,7 +72,8 @@
382 theSerializer(0),
383 theItemHandler(aHandler),
384 theCallbackData(aCallBackData),
385- theLastContinuationCommand()
386+ theLastContinuationCommand(),
387+ theInterruptBreak(aInterruptBreak)
388 {
389 }
390
391@@ -320,6 +322,7 @@
392 if (theExecStatus != QUERY_SUSPENDED) {
393 return;
394 }
395+ *theInterruptBreak = false;
396 theExecStatus = QUERY_RUNNING;
397 resume();
398 }
399@@ -500,6 +503,17 @@
400 }
401
402
403+bool
404+DebuggerRuntime::getAndClearInterruptBreak()
405+{
406+ bool lMustBreak = *theInterruptBreak;
407+ if (lMustBreak) {
408+ *theInterruptBreak = false;
409+ }
410+ return lMustBreak;
411+}
412+
413+
414 std::list<std::pair<zstring, zstring> >
415 DebuggerRuntime::eval(zstring& aExpr)
416 {
417@@ -700,7 +714,8 @@
418 theSerializerOptions,
419 theCommunicator,
420 theItemHandler,
421- theCallbackData);
422+ theCallbackData,
423+ theInterruptBreak);
424
425 lNewRuntime->theBreakpoints = theBreakpoints;
426 return lNewRuntime;
427
428=== modified file 'src/debugger/debugger_runtime.h'
429--- src/debugger/debugger_runtime.h 2012-01-11 17:30:25 +0000
430+++ src/debugger/debugger_runtime.h 2012-01-18 12:03:24 +0000
431@@ -48,11 +48,12 @@
432
433 DebuggerRuntime(
434 XQueryImpl* xqueryImpl,
435- std::ostream& oStream,
436+ std::ostream& outStream,
437 Zorba_SerializerOptions& serializerOptions,
438 DebuggerCommunicator* communicator,
439- itemHandler aHandler,
440- void* aCallBackData);
441+ itemHandler handler,
442+ void* callBackData,
443+ bool* interruptBreak);
444
445 virtual ~DebuggerRuntime();
446
447@@ -151,6 +152,9 @@
448 void
449 stepOut();
450
451+ bool
452+ getAndClearInterruptBreak();
453+
454 DebuggerRuntime*
455 clone();
456
457@@ -174,8 +178,8 @@
458 serializer* theSerializer;
459 itemHandler theItemHandler;
460 void* theCallbackData;
461-
462 std::pair<int, std::string> theLastContinuationCommand;
463+ bool* theInterruptBreak;
464 };
465 }
466
467
468=== modified file 'src/debugger/debugger_server.cpp'
469--- src/debugger/debugger_server.cpp 2012-01-11 17:30:25 +0000
470+++ src/debugger/debugger_server.cpp 2012-01-18 12:03:24 +0000
471@@ -18,6 +18,9 @@
472 #include "debugger_server.h"
473
474 #include <sstream>
475+#ifndef WIN32
476+# include <signal.h>
477+#endif
478
479 #include <zorba/base64.h>
480 #include <zorba/util/uri.h>
481@@ -34,6 +37,31 @@
482
483 namespace zorba {
484
485+bool theInterruptBreak = false;
486+
487+// this will make sure the zorba when run in debug (i.e. with the debugger)
488+// will not terminate when Ctrl-C is pressed but trigger a query interruption
489+// break through the theInterruptBreak variable that is continuously monitored
490+// by the debugger commons through the debugger runtime
491+#ifdef WIN32
492+BOOL WINAPI
493+DebuggerServer::ctrlC_Handler(DWORD aCtrlType)
494+{
495+ if (CTRL_C_EVENT == aCtrlType) {
496+ theInterruptBreak = true;
497+ return true;
498+ }
499+ return false;
500+}
501+#else
502+void
503+DebuggerServer::ctrlC_Handler(int lParam)
504+{
505+ theInterruptBreak = true;
506+}
507+#endif
508+
509+
510 DebuggerServer::DebuggerServer(
511 XQueryImpl* aQuery,
512 Zorba_SerializerOptions& aSerializerOptions,
513@@ -47,7 +75,8 @@
514 theCommunicator = new DebuggerCommunicator(aHost, aPort);
515 theRuntime = new DebuggerRuntime(
516 aQuery, aOstream, aSerializerOptions,
517- theCommunicator, aHandler, aCallbackData);
518+ theCommunicator, aHandler, aCallbackData,
519+ &theInterruptBreak);
520 #ifdef WIN32
521 theFileName = aQuery->getFileName().str();
522 #else
523@@ -66,9 +95,17 @@
524 delete theCommunicator;
525 }
526
527+
528 bool
529 DebuggerServer::run()
530 {
531+ // add the interrupt handlers to catch Ctrl-C
532+ #ifdef WIN32
533+ SetConsoleCtrlHandler(DebuggerServer::ctrlC_Handler, TRUE);
534+ #else
535+ signal(SIGINT, ctrlC_Handler);
536+ #endif
537+
538 theCommunicator->connect();
539
540 if (!theCommunicator->isConnected()) {
541
542=== modified file 'src/debugger/debugger_server.h'
543--- src/debugger/debugger_server.h 2011-12-21 14:40:33 +0000
544+++ src/debugger/debugger_server.h 2012-01-18 12:03:24 +0000
545@@ -59,6 +59,15 @@
546
547 private:
548
549+#ifdef WIN32
550+ static BOOL WINAPI
551+ ctrlC_Handler(DWORD aCtrlType);
552+#else
553+ static void
554+ ctrlC_Handler(int lParam);
555+#endif
556+
557+
558 std::string
559 processCommand(DebuggerCommand command);
560

Subscribers

People subscribed via source and target branches