]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/test/impl/debug.ipp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / test / impl / debug.ipp
CommitLineData
7c673cae
FG
1// (C) Copyright Gennadiy Rozental 2001.
2// Use, modification, and distribution are subject to the
3// Boost Software License, Version 1.0. (See accompanying file
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision$
11//
12// Description : debug interfaces implementation
13// ***************************************************************************
14
15#ifndef BOOST_TEST_DEBUG_API_IPP_112006GER
16#define BOOST_TEST_DEBUG_API_IPP_112006GER
17
18// Boost.Test
19#include <boost/test/detail/config.hpp>
7c673cae
FG
20#include <boost/test/detail/global_typedef.hpp>
21
22#include <boost/test/debug.hpp>
23#include <boost/test/debug_config.hpp>
24
92f5a8d4
TL
25#include <boost/core/ignore_unused.hpp>
26
7c673cae
FG
27// Implementation on Windows
28#if defined(_WIN32) && !defined(UNDER_CE) && !defined(BOOST_DISABLE_WIN32) // ******* WIN32
29
30# define BOOST_WIN32_BASED_DEBUG
31
32// SYSTEM API
33# include <windows.h>
34# include <winreg.h>
35# include <cstdio>
36# include <cstring>
37
38# if !defined(NDEBUG) && defined(_MSC_VER)
39# define BOOST_MS_CRT_BASED_DEBUG
40# include <crtdbg.h>
41# endif
42
43
44# ifdef BOOST_NO_STDC_NAMESPACE
45namespace std { using ::memset; using ::sprintf; }
46# endif
47
48#elif defined(unix) || defined(__unix) // ********************* UNIX
49
50# define BOOST_UNIX_BASED_DEBUG
51
52// Boost.Test
53#include <boost/test/utils/class_properties.hpp>
54#include <boost/test/utils/algorithm.hpp>
55
56// STL
57#include <cstring> // std::memcpy
58#include <map>
59#include <cstdio>
60#include <stdarg.h> // !! ?? cstdarg
61
62// SYSTEM API
63# include <unistd.h>
64# include <signal.h>
65# include <fcntl.h>
66
67# include <sys/types.h>
68# include <sys/stat.h>
69# include <sys/wait.h>
70# include <sys/time.h>
71# include <stdio.h>
72# include <stdlib.h>
73
74# if defined(sun) || defined(__sun)
75
76# define BOOST_SUN_BASED_DEBUG
77
78# ifndef BOOST_TEST_DBG_LIST
79# define BOOST_TEST_DBG_LIST dbx;gdb
80# endif
81
82# define BOOST_TEST_CNL_DBG dbx
83# define BOOST_TEST_GUI_DBG dbx-ddd
84
85# include <procfs.h>
86
f67539c2 87# elif defined(linux) || defined(__linux__)
7c673cae
FG
88
89# define BOOST_LINUX_BASED_DEBUG
90
91# include <sys/ptrace.h>
92
93# ifndef BOOST_TEST_STAT_LINE_MAX
94# define BOOST_TEST_STAT_LINE_MAX 500
95# endif
96
97# ifndef BOOST_TEST_DBG_LIST
98# define BOOST_TEST_DBG_LIST gdb
99# endif
100
101# define BOOST_TEST_CNL_DBG gdb
102# define BOOST_TEST_GUI_DBG gdb-xterm
103
104# endif
105
106#endif
107
108#include <boost/test/detail/suppress_warnings.hpp>
109
110//____________________________________________________________________________//
111
112namespace boost {
113namespace debug {
114
115using unit_test::const_string;
116
117// ************************************************************************** //
118// ************** debug::info_t ************** //
119// ************************************************************************** //
120
121namespace {
122
123#if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
124
125template<typename T>
126inline void
127dyn_symbol( T& res, char const* module_name, char const* symbol_name )
128{
129 HMODULE m = ::GetModuleHandleA( module_name );
130
131 if( !m )
132 m = ::LoadLibraryA( module_name );
133
134 res = reinterpret_cast<T>( ::GetProcAddress( m, symbol_name ) );
135}
136
137//____________________________________________________________________________//
138
139static struct info_t {
140 typedef BOOL (WINAPI* IsDebuggerPresentT)();
141 typedef LONG (WINAPI* RegQueryValueExT)( HKEY, char const* /*LPTSTR*/, LPDWORD, LPDWORD, LPBYTE, LPDWORD );
142 typedef LONG (WINAPI* RegOpenKeyT)( HKEY, char const* /*LPCTSTR*/, PHKEY );
143 typedef LONG (WINAPI* RegCloseKeyT)( HKEY );
144
145 info_t();
146
147 IsDebuggerPresentT m_is_debugger_present;
148 RegOpenKeyT m_reg_open_key;
149 RegQueryValueExT m_reg_query_value;
150 RegCloseKeyT m_reg_close_key;
151
152} s_info;
153
154//____________________________________________________________________________//
155
156info_t::info_t()
157{
158 dyn_symbol( m_is_debugger_present, "kernel32", "IsDebuggerPresent" );
159 dyn_symbol( m_reg_open_key, "advapi32", "RegOpenKeyA" );
160 dyn_symbol( m_reg_query_value, "advapi32", "RegQueryValueExA" );
161 dyn_symbol( m_reg_close_key, "advapi32", "RegCloseKey" );
162}
163
164//____________________________________________________________________________//
165
166#elif defined(BOOST_UNIX_BASED_DEBUG)
167
168// ************************************************************************** //
169// ************** fd_holder ************** //
170// ************************************************************************** //
171
172struct fd_holder {
173 explicit fd_holder( int fd ) : m_fd( fd ) {}
174 ~fd_holder()
175 {
176 if( m_fd != -1 )
177 ::close( m_fd );
178 }
179
180 operator int() { return m_fd; }
181
182private:
183 // Data members
184 int m_fd;
185};
186
187
188// ************************************************************************** //
189// ************** process_info ************** //
190// ************************************************************************** //
191
192struct process_info {
193 // Constructor
194 explicit process_info( int pid );
195
196 // access methods
197 int parent_pid() const { return m_parent_pid; }
198 const_string binary_name() const { return m_binary_name; }
199 const_string binary_path() const { return m_binary_path; }
200
201private:
202 // Data members
203 int m_parent_pid;
204 const_string m_binary_name;
205 const_string m_binary_path;
206
207#if defined(BOOST_SUN_BASED_DEBUG)
208 struct psinfo m_psi;
209 char m_binary_path_buff[500+1]; // !! ??
210#elif defined(BOOST_LINUX_BASED_DEBUG)
211 char m_stat_line[BOOST_TEST_STAT_LINE_MAX+1];
212 char m_binary_path_buff[500+1]; // !! ??
213#endif
214};
215
216//____________________________________________________________________________//
217
218process_info::process_info( int pid )
219: m_parent_pid( 0 )
220{
221#if defined(BOOST_SUN_BASED_DEBUG)
222 char fname_buff[30];
223
224 ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/psinfo", pid );
225
226 fd_holder psinfo_fd( ::open( fname_buff, O_RDONLY ) );
227
228 if( psinfo_fd == -1 )
229 return;
230
231 if( ::read( psinfo_fd, &m_psi, sizeof(m_psi) ) == -1 )
232 return;
233
234 m_parent_pid = m_psi.pr_ppid;
235
236 m_binary_name.assign( m_psi.pr_fname );
237
238 //-------------------------- //
239
240 ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/as", pid );
241
242 fd_holder as_fd( ::open( fname_buff, O_RDONLY ) );
243 uintptr_t binary_name_pos;
244
245 // !! ?? could we avoid reading whole m_binary_path_buff?
246 if( as_fd == -1 ||
247 ::lseek( as_fd, m_psi.pr_argv, SEEK_SET ) == -1 ||
248 ::read ( as_fd, &binary_name_pos, sizeof(binary_name_pos) ) == -1 ||
249 ::lseek( as_fd, binary_name_pos, SEEK_SET ) == -1 ||
250 ::read ( as_fd, m_binary_path_buff, sizeof(m_binary_path_buff) ) == -1 )
251 return;
252
253 m_binary_path.assign( m_binary_path_buff );
254
255#elif defined(BOOST_LINUX_BASED_DEBUG)
256 char fname_buff[30];
257
258 ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/stat", pid );
259
260 fd_holder psinfo_fd( ::open( fname_buff, O_RDONLY ) );
261
262 if( psinfo_fd == -1 )
263 return;
264
265 ssize_t num_read = ::read( psinfo_fd, m_stat_line, sizeof(m_stat_line)-1 );
266 if( num_read == -1 )
267 return;
268
269 m_stat_line[num_read] = 0;
270
271 char const* name_beg = m_stat_line;
272 while( *name_beg && *name_beg != '(' )
273 ++name_beg;
274
275 char const* name_end = name_beg+1;
276 while( *name_end && *name_end != ')' )
277 ++name_end;
278
279 std::sscanf( name_end+1, "%*s%d", &m_parent_pid );
280
281 m_binary_name.assign( name_beg+1, name_end );
282
283 ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/exe", pid );
284 num_read = ::readlink( fname_buff, m_binary_path_buff, sizeof(m_binary_path_buff)-1 );
285
286 if( num_read == -1 )
287 return;
288
289 m_binary_path_buff[num_read] = 0;
290 m_binary_path.assign( m_binary_path_buff, num_read );
291#endif
292}
293
294//____________________________________________________________________________//
295
296// ************************************************************************** //
297// ************** prepare_window_title ************** //
298// ************************************************************************** //
299
300static char*
301prepare_window_title( dbg_startup_info const& dsi )
302{
303 typedef unit_test::const_string str_t;
304
305 static char title_str[50];
306
307 str_t path_sep( "\\/" );
308
309 str_t::iterator it = unit_test::utils::find_last_of( dsi.binary_path.begin(), dsi.binary_path.end(),
310 path_sep.begin(), path_sep.end() );
311
312 if( it == dsi.binary_path.end() )
313 it = dsi.binary_path.begin();
314 else
315 ++it;
316
317 ::snprintf( title_str, sizeof(title_str), "%*s %ld", (int)(dsi.binary_path.end()-it), it, dsi.pid );
318
319 return title_str;
320}
321
322//____________________________________________________________________________//
323
324// ************************************************************************** //
325// ************** save_execlp ************** //
326// ************************************************************************** //
327
328typedef unit_test::basic_cstring<char> mbuffer;
329
330inline char*
331copy_arg( mbuffer& dest, const_string arg )
332{
333 if( dest.size() < arg.size()+1 )
334 return 0;
335
336 char* res = dest.begin();
337
338 std::memcpy( res, arg.begin(), arg.size()+1 );
339
340 dest.trim_left( arg.size()+1 );
341
342 return res;
343}
344
345//____________________________________________________________________________//
346
347bool
348safe_execlp( char const* file, ... )
349{
350 static char* argv_buff[200];
351
352 va_list args;
353 char const* arg;
354
355 // first calculate actual number of arguments
356 int num_args = 2; // file name and 0 at least
357
358 va_start( args, file );
359 while( !!(arg = va_arg( args, char const* )) )
360 num_args++;
361 va_end( args );
362
363 // reserve space for the argument pointers array
364 char** argv_it = argv_buff;
365 mbuffer work_buff( reinterpret_cast<char*>(argv_buff), sizeof(argv_buff) );
366 work_buff.trim_left( num_args * sizeof(char*) );
367
368 // copy all the argument values into local storage
369 if( !(*argv_it++ = copy_arg( work_buff, file )) )
370 return false;
371
372 printf( "!! %s\n", file );
373
374 va_start( args, file );
375 while( !!(arg = va_arg( args, char const* )) ) {
376 printf( "!! %s\n", arg );
377 if( !(*argv_it++ = copy_arg( work_buff, arg )) ) {
378 va_end( args );
379 return false;
380 }
381 }
382 va_end( args );
383
384 *argv_it = 0;
385
386 return ::execvp( file, argv_buff ) != -1;
387}
388
389//____________________________________________________________________________//
390
391// ************************************************************************** //
392// ************** start_debugger_in_emacs ************** //
393// ************************************************************************** //
394
395static void
396start_debugger_in_emacs( dbg_startup_info const& dsi, char const* emacs_name, char const* dbg_command )
397{
398 char const* title = prepare_window_title( dsi );
399
400 if( !title )
401 return;
402
403 dsi.display.is_empty()
404 ? safe_execlp( emacs_name, "-title", title, "--eval", dbg_command, 0 )
405 : safe_execlp( emacs_name, "-title", title, "-display", dsi.display.begin(), "--eval", dbg_command, 0 );
406}
407
408//____________________________________________________________________________//
409
410// ************************************************************************** //
411// ************** gdb starters ************** //
412// ************************************************************************** //
413
414static char const*
415prepare_gdb_cmnd_file( dbg_startup_info const& dsi )
416{
417 // prepare pid value
418 char pid_buff[16];
419 ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
420 unit_test::const_string pid_str( pid_buff );
421
422 static char cmd_file_name[] = "/tmp/btl_gdb_cmd_XXXXXX"; // !! ??
423
424 // prepare commands
f67539c2 425 const mode_t cur_umask = ::umask( S_IRWXO | S_IRWXG );
7c673cae 426 fd_holder cmd_fd( ::mkstemp( cmd_file_name ) );
f67539c2 427 ::umask( cur_umask );
7c673cae
FG
428
429 if( cmd_fd == -1 )
430 return 0;
431
432#define WRITE_STR( str ) if( ::write( cmd_fd, str.begin(), str.size() ) == -1 ) return 0;
433#define WRITE_CSTR( str ) if( ::write( cmd_fd, str, sizeof( str )-1 ) == -1 ) return 0;
434
435 WRITE_CSTR( "file " );
436 WRITE_STR( dsi.binary_path );
437 WRITE_CSTR( "\nattach " );
438 WRITE_STR( pid_str );
439 WRITE_CSTR( "\nshell unlink " );
440 WRITE_STR( dsi.init_done_lock );
441 WRITE_CSTR( "\ncont" );
442 if( dsi.break_or_continue )
443 WRITE_CSTR( "\nup 4" );
444
445 WRITE_CSTR( "\necho \\n" ); // !! ??
446 WRITE_CSTR( "\nlist -" );
447 WRITE_CSTR( "\nlist" );
448 WRITE_CSTR( "\nshell unlink " );
449 WRITE_CSTR( cmd_file_name );
450
451 return cmd_file_name;
452}
453
454//____________________________________________________________________________//
455
456static void
457start_gdb_in_console( dbg_startup_info const& dsi )
458{
459 char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
460
461 if( !cmnd_file_name )
462 return;
463
464 safe_execlp( "gdb", "-q", "-x", cmnd_file_name, 0 );
465}
466
467//____________________________________________________________________________//
468
469static void
470start_gdb_in_xterm( dbg_startup_info const& dsi )
471{
472 char const* title = prepare_window_title( dsi );
473 char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
474
475 if( !title || !cmnd_file_name )
476 return;
477
478 safe_execlp( "xterm", "-T", title, "-display", dsi.display.begin(),
479 "-bg", "black", "-fg", "white", "-geometry", "88x30+10+10", "-fn", "9x15", "-e",
480 "gdb", "-q", "-x", cmnd_file_name, 0 );
481}
482
483//____________________________________________________________________________//
484
485static void
486start_gdb_in_emacs( dbg_startup_info const& dsi )
487{
488 char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
489 if( !cmnd_file_name )
490 return;
491
492 char dbg_cmd_buff[500]; // !! ??
493 ::snprintf( dbg_cmd_buff, sizeof(dbg_cmd_buff), "(progn (gdb \"gdb -q -x %s\"))", cmnd_file_name );
494
495 start_debugger_in_emacs( dsi, "emacs", dbg_cmd_buff );
496}
497
498//____________________________________________________________________________//
499
500static void
501start_gdb_in_xemacs( dbg_startup_info const& )
502{
503 // !! ??
504}
505
506//____________________________________________________________________________//
507
508// ************************************************************************** //
509// ************** dbx starters ************** //
510// ************************************************************************** //
511
512static char const*
513prepare_dbx_cmd_line( dbg_startup_info const& dsi, bool list_source = true )
514{
515 static char cmd_line_buff[500]; // !! ??
516
517 ::snprintf( cmd_line_buff, sizeof(cmd_line_buff), "unlink %s;cont;%s%s",
518 dsi.init_done_lock.begin(),
519 dsi.break_or_continue ? "up 2;": "",
520 list_source ? "echo \" \";list -w3;" : "" );
521
522 return cmd_line_buff;
523}
524
525//____________________________________________________________________________//
526
527static void
528start_dbx_in_console( dbg_startup_info const& dsi )
529{
530 char pid_buff[16];
531 ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
532
533 safe_execlp( "dbx", "-q", "-c", prepare_dbx_cmd_line( dsi ), dsi.binary_path.begin(), pid_buff, 0 );
534}
535
536//____________________________________________________________________________//
537
538static void
539start_dbx_in_xterm( dbg_startup_info const& dsi )
540{
541 char const* title = prepare_window_title( dsi );
542 if( !title )
543 return;
544
545 char pid_buff[16]; // !! ??
546 ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
547
548 safe_execlp( "xterm", "-T", title, "-display", dsi.display.begin(),
549 "-bg", "black", "-fg", "white", "-geometry", "88x30+10+10", "-fn", "9x15", "-e",
550 "dbx", "-q", "-c", prepare_dbx_cmd_line( dsi ), dsi.binary_path.begin(), pid_buff, 0 );
551}
552
553//____________________________________________________________________________//
554
555static void
556start_dbx_in_emacs( dbg_startup_info const& /*dsi*/ )
557{
558// char dbg_cmd_buff[500]; // !! ??
559//
560// ::snprintf( dbg_cmd_buff, sizeof(dbg_cmd_buff), "(progn (dbx \"dbx -q -c cont %s %ld\"))", dsi.binary_path.begin(), dsi.pid );
561
562// start_debugger_in_emacs( dsi, "emacs", dbg_cmd_buff );
563}
564
565//____________________________________________________________________________//
566
567static void
568start_dbx_in_xemacs( dbg_startup_info const& )
569{
570 // !! ??
571}
572
573//____________________________________________________________________________//
574
575static void
576start_dbx_in_ddd( dbg_startup_info const& dsi )
577{
578 char const* title = prepare_window_title( dsi );
579 if( !title )
580 return;
581
582 char pid_buff[16]; // !! ??
583 ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
584
585 safe_execlp( "ddd", "-display", dsi.display.begin(),
586 "--dbx", "-q", "-c", prepare_dbx_cmd_line( dsi, false ), dsi.binary_path.begin(), pid_buff, 0 );
587}
588
589//____________________________________________________________________________//
590
591// ************************************************************************** //
592// ************** debug::info_t ************** //
593// ************************************************************************** //
594
595static struct info_t {
596 // Constructor
597 info_t();
598
599 // Public properties
600 unit_test::readwrite_property<std::string> p_dbg;
601
602 // Data members
603 std::map<std::string,dbg_starter> m_dbg_starter_reg;
604} s_info;
605
606//____________________________________________________________________________//
607
608info_t::info_t()
609{
610 p_dbg.value = ::getenv( "DISPLAY" )
611 ? std::string( BOOST_STRINGIZE( BOOST_TEST_GUI_DBG ) )
612 : std::string( BOOST_STRINGIZE( BOOST_TEST_CNL_DBG ) );
613
614 m_dbg_starter_reg[std::string("gdb")] = &start_gdb_in_console;
615 m_dbg_starter_reg[std::string("gdb-emacs")] = &start_gdb_in_emacs;
616 m_dbg_starter_reg[std::string("gdb-xterm")] = &start_gdb_in_xterm;
617 m_dbg_starter_reg[std::string("gdb-xemacs")] = &start_gdb_in_xemacs;
618
619 m_dbg_starter_reg[std::string("dbx")] = &start_dbx_in_console;
620 m_dbg_starter_reg[std::string("dbx-emacs")] = &start_dbx_in_emacs;
621 m_dbg_starter_reg[std::string("dbx-xterm")] = &start_dbx_in_xterm;
622 m_dbg_starter_reg[std::string("dbx-xemacs")] = &start_dbx_in_xemacs;
623 m_dbg_starter_reg[std::string("dbx-ddd")] = &start_dbx_in_ddd;
624}
625
626//____________________________________________________________________________//
627
628#endif
629
630} // local namespace
631
632// ************************************************************************** //
633// ************** check if program is running under debugger ************** //
634// ************************************************************************** //
635
636bool
637under_debugger()
638{
639#if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
640
641 return !!s_info.m_is_debugger_present && s_info.m_is_debugger_present();
642
643#elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
644
645 // !! ?? could/should we cache the result somehow?
646 const_string dbg_list = BOOST_TEST_STRINGIZE( BOOST_TEST_DBG_LIST );
647
648 pid_t pid = ::getpid();
649
650 while( pid != 0 ) {
651 process_info pi( pid );
652
653 // !! ?? should we use tokenizer here instead?
654 if( dbg_list.find( pi.binary_name() ) != const_string::npos )
655 return true;
656
657 pid = (pi.parent_pid() == pid ? 0 : pi.parent_pid());
658 }
659
660 return false;
661
662#else // ****************************************************** default
663
664 return false;
665
666#endif
667}
668
669//____________________________________________________________________________//
670
671// ************************************************************************** //
672// ************** cause program to break execution ************** //
673// ************** in debugger at call point ************** //
674// ************************************************************************** //
675
676void
677debugger_break()
678{
679 // !! ?? auto-start debugger?
680
681#if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
682
683#if defined(__GNUC__) && !defined(__MINGW32__) || \
684 defined(__INTEL_COMPILER)
685# define BOOST_DEBUG_BREAK __debugbreak
686#else
687# define BOOST_DEBUG_BREAK DebugBreak
688#endif
689
690#ifndef __MINGW32__
691 if( !under_debugger() ) {
692 __try {
693 __try {
694 BOOST_DEBUG_BREAK();
695 }
696 __except( UnhandledExceptionFilter(GetExceptionInformation()) )
697 {
698 // User opted to ignore the breakpoint
699 return;
700 }
701 }
702 __except (EXCEPTION_EXECUTE_HANDLER)
703 {
704 // If we got here, the user has pushed Debug. Debugger is already attached to our process and we
705 // continue to let the another BOOST_DEBUG_BREAK to be called.
706 }
707 }
708#endif
709
710 BOOST_DEBUG_BREAK();
711
712#elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
713
714 ::kill( ::getpid(), SIGTRAP );
715
716#else // ****************************************************** default
717
718#endif
719}
720
721//____________________________________________________________________________//
722
723// ************************************************************************** //
724// ************** console debugger setup ************** //
725// ************************************************************************** //
726
727#if defined(BOOST_UNIX_BASED_DEBUG) // ************************ UNIX
728
729std::string
730set_debugger( unit_test::const_string dbg_id, dbg_starter s )
731{
732 std::string old = s_info.p_dbg;
733
734 assign_op( s_info.p_dbg.value, dbg_id, 0 );
735
736 if( !!s )
737 s_info.m_dbg_starter_reg[s_info.p_dbg.get()] = s;
738
739 return old;
740}
741
742#else // ***************************************************** default
743
744std::string
745set_debugger( unit_test::const_string, dbg_starter )
746{
747 return std::string();
748}
749
750#endif
751
752//____________________________________________________________________________//
753
754// ************************************************************************** //
755// ************** attach debugger to the current process ************** //
756// ************************************************************************** //
757
758#if defined(BOOST_WIN32_BASED_DEBUG)
759
760struct safe_handle_helper
761{
762 HANDLE& handle;
763 safe_handle_helper(HANDLE &handle_) : handle(handle_) {}
764
765 void close_handle()
766 {
767 if( handle != INVALID_HANDLE_VALUE )
768 {
769 ::CloseHandle( handle );
770 handle = INVALID_HANDLE_VALUE;
771 }
772 }
773
92f5a8d4 774 ~safe_handle_helper()
7c673cae
FG
775 {
776 close_handle();
777 }
778};
779#endif
780
781bool
782attach_debugger( bool break_or_continue )
783{
784 if( under_debugger() )
785 return false;
786
787#if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
788
789 const int MAX_CMD_LINE = 200;
790
791 // *************************************************** //
792 // Debugger "ready" event
793
794 SECURITY_ATTRIBUTES attr;
795 attr.nLength = sizeof(attr);
796 attr.lpSecurityDescriptor = NULL;
797 attr.bInheritHandle = true;
798
799 // manual resettable, initially non signaled, unnamed event,
800 // that will signal me that debugger initialization is done
801 HANDLE dbg_init_done_ev = ::CreateEvent(
802 &attr, // pointer to security attributes
803 true, // flag for manual-reset event
804 false, // flag for initial state
805 NULL // pointer to event-object name
806 );
807
808 if( !dbg_init_done_ev )
809 return false;
92f5a8d4 810
7c673cae
FG
811 safe_handle_helper safe_handle_obj( dbg_init_done_ev );
812
813 // *************************************************** //
814 // Debugger command line format
815
816 HKEY reg_key;
817
818 if( !s_info.m_reg_open_key || (*s_info.m_reg_open_key)(
819 HKEY_LOCAL_MACHINE, // handle of open key
820 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", // name of subkey to open
821 &reg_key ) != ERROR_SUCCESS ) // address of handle of open key
822 return false;
823
824 char format[MAX_CMD_LINE];
825 DWORD format_size = MAX_CMD_LINE;
826 DWORD type = REG_SZ;
827
92f5a8d4 828 bool b_read_key = s_info.m_reg_query_value &&
7c673cae
FG
829 ((*s_info.m_reg_query_value)(
830 reg_key, // handle of open key
831 "Debugger", // name of subkey to query
832 0, // reserved
833 &type, // value type
834 (LPBYTE)format, // buffer for returned string
835 &format_size ) == ERROR_SUCCESS ); // in: buffer size; out: actual size of returned string
836
837 if( !s_info.m_reg_close_key || (*s_info.m_reg_close_key)( reg_key ) != ERROR_SUCCESS )
838 return false;
92f5a8d4 839
7c673cae
FG
840 if( !b_read_key )
841 return false;
842
843 // *************************************************** //
844 // Debugger command line
845
846 char cmd_line[MAX_CMD_LINE];
847 std::sprintf( cmd_line, format, ::GetCurrentProcessId(), dbg_init_done_ev );
848
849 // *************************************************** //
850 // Debugger window parameters
851
852 STARTUPINFOA startup_info;
853 std::memset( &startup_info, 0, sizeof(startup_info) );
854
855 startup_info.cb = sizeof(startup_info);
856 startup_info.dwFlags = STARTF_USESHOWWINDOW;
857 startup_info.wShowWindow = SW_SHOWNORMAL;
858
859 // debugger process s_info
860 PROCESS_INFORMATION debugger_info;
861
862 bool created = !!::CreateProcessA(
863 NULL, // pointer to name of executable module; NULL - use the one in command line
864 cmd_line, // pointer to command line string
865 NULL, // pointer to process security attributes; NULL - debugger's handle can't be inherited
866 NULL, // pointer to thread security attributes; NULL - debugger's handle can't be inherited
867 true, // debugger inherit opened handles
868 0, // priority flags; 0 - normal priority
869 NULL, // pointer to new environment block; NULL - use this process environment
870 NULL, // pointer to current directory name; NULL - use this process correct directory
871 &startup_info, // pointer to STARTUPINFO that specifies main window appearance
872 &debugger_info // pointer to PROCESS_INFORMATION that will contain the new process identification
873 );
874
875 bool debugger_run_ok = false;
876 if( created )
877 {
878 DWORD ret_code = ::WaitForSingleObject( dbg_init_done_ev, INFINITE );
879 debugger_run_ok = ( ret_code == WAIT_OBJECT_0 );
880 }
881
882 safe_handle_obj.close_handle();
883
884 if( !created || !debugger_run_ok )
885 return false;
886
887 if( break_or_continue )
888 debugger_break();
889
890 return true;
891
892#elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
893
894 char init_done_lock_fn[] = "/tmp/btl_dbg_init_done_XXXXXX";
f67539c2 895 const mode_t cur_umask = ::umask( S_IRWXO | S_IRWXG );
7c673cae 896 fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
f67539c2 897 ::umask( cur_umask );
7c673cae
FG
898
899 if( init_done_lock_fd == -1 )
900 return false;
901
902 pid_t child_pid = fork();
903
904 if( child_pid == -1 )
905 return false;
906
907 if( child_pid != 0 ) { // parent process - here we will start the debugger
908 dbg_startup_info dsi;
909
910 process_info pi( child_pid );
911 if( pi.binary_path().is_empty() )
912 ::exit( -1 );
913
914 dsi.pid = child_pid;
915 dsi.break_or_continue = break_or_continue;
916 dsi.binary_path = pi.binary_path();
917 dsi.display = ::getenv( "DISPLAY" );
918 dsi.init_done_lock = init_done_lock_fn;
919
920 dbg_starter starter = s_info.m_dbg_starter_reg[s_info.p_dbg];
921 if( !!starter )
922 starter( dsi );
923
924 ::perror( "Boost.Test execution monitor failed to start a debugger:" );
925
926 ::exit( -1 );
927 }
928
929 // child process - here we will continue our test module execution ; // !! ?? should it be vice versa
930
931 while( ::access( init_done_lock_fn, F_OK ) == 0 ) {
932 struct timeval to = { 0, 100 };
933
934 ::select( 0, 0, 0, 0, &to );
935 }
936
937// char dummy;
938// while( ::read( init_done_lock_fd, &dummy, sizeof(char) ) == 0 );
939
940 if( break_or_continue )
941 debugger_break();
942
943 return true;
944
945#else // ****************************************************** default
11fdf7f2 946 (void) break_or_continue; // silence 'unused variable' warning
7c673cae
FG
947 return false;
948
949#endif
950}
951
952//____________________________________________________________________________//
953
954// ************************************************************************** //
955// ************** switch on/off detect memory leaks feature ************** //
956// ************************************************************************** //
957
958void
959detect_memory_leaks( bool on_off, unit_test::const_string report_file )
960{
92f5a8d4 961 boost::ignore_unused( on_off );
7c673cae
FG
962
963#ifdef BOOST_MS_CRT_BASED_DEBUG
964 int flags = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
965
966 if( !on_off )
967 flags &= ~_CRTDBG_LEAK_CHECK_DF;
968 else {
969 flags |= _CRTDBG_LEAK_CHECK_DF;
970 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
971
972 if( report_file.is_empty() )
973 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
974 else {
975 HANDLE hreport_f = ::CreateFileA( report_file.begin(),
976 GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
977 _CrtSetReportFile(_CRT_WARN, hreport_f );
978 }
979 }
980
981 _CrtSetDbgFlag ( flags );
982#else
92f5a8d4 983 boost::ignore_unused( report_file );
7c673cae
FG
984#endif // BOOST_MS_CRT_BASED_DEBUG
985}
986
987//____________________________________________________________________________//
988
989// ************************************************************************** //
990// ************** cause program to break execution in ************** //
991// ************** debugger at specific allocation point ************** //
992// ************************************************************************** //
993
994void
995break_memory_alloc( long mem_alloc_order_num )
996{
92f5a8d4 997 boost::ignore_unused( mem_alloc_order_num );
7c673cae
FG
998
999#ifdef BOOST_MS_CRT_BASED_DEBUG
1000 // only set the value if one was supplied (do not use default used by UTF just as a indicator to enable leak detection)
1001 if( mem_alloc_order_num > 1 )
1002 _CrtSetBreakAlloc( mem_alloc_order_num );
1003#endif // BOOST_MS_CRT_BASED_DEBUG
1004}
1005
1006//____________________________________________________________________________//
1007
1008} // namespace debug
1009} // namespace boost
1010
1011#include <boost/test/detail/enable_warnings.hpp>
1012
1013#endif // BOOST_TEST_DEBUG_API_IPP_112006GER