debug.ipp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. // (C) Copyright Gennadiy Rozental 2006-2008.
  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. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 57992 $
  10. //
  11. // Description : debug interfaces implementation
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_DEBUG_API_IPP_112006GER
  14. #define BOOST_TEST_DEBUG_API_IPP_112006GER
  15. // Boost.Test
  16. #include <boost/test/detail/config.hpp>
  17. #include <boost/test/detail/workaround.hpp>
  18. #include <boost/test/detail/global_typedef.hpp>
  19. #include <boost/test/debug.hpp>
  20. #include <boost/test/debug_config.hpp>
  21. // Implementation on Windows
  22. #if defined(_WIN32) && !defined(UNDER_CE) && !defined(BOOST_DISABLE_WIN32) // ******* WIN32
  23. # define BOOST_WIN32_BASED_DEBUG
  24. // SYSTEM API
  25. # include <windows.h>
  26. # include <winreg.h>
  27. # include <cstdio>
  28. # include <cstring>
  29. # if !defined(NDEBUG) && defined(_MSC_VER)
  30. # define BOOST_MS_CRT_BASED_DEBUG
  31. # include <crtdbg.h>
  32. # endif
  33. # if BOOST_WORKAROUND( BOOST_MSVC, <1300)
  34. # define snprintf _snprintf
  35. # endif
  36. # ifdef BOOST_NO_STDC_NAMESPACE
  37. namespace std { using ::memset; using ::sprintf; }
  38. # endif
  39. #elif defined(unix) || defined(__unix) // ********************* UNIX
  40. # define BOOST_UNIX_BASED_DEBUG
  41. // Boost.Test
  42. #include <boost/test/utils/class_properties.hpp>
  43. #include <boost/test/utils/algorithm.hpp>
  44. // STL
  45. #include <cstring> // std::memcpy
  46. #include <map>
  47. #include <cstdio>
  48. #include <stdarg.h> // !! ?? cstdarg
  49. // SYSTEM API
  50. # include <unistd.h>
  51. # include <signal.h>
  52. # include <fcntl.h>
  53. # include <sys/types.h>
  54. # include <sys/stat.h>
  55. # include <sys/wait.h>
  56. # include <sys/time.h>
  57. # include <stdio.h>
  58. # include <stdlib.h>
  59. # if defined(sun) || defined(__sun)
  60. # define BOOST_SUN_BASED_DEBUG
  61. # ifndef BOOST_TEST_DBG_LIST
  62. # define BOOST_TEST_DBG_LIST dbx;gdb
  63. # endif
  64. # define BOOST_TEST_CNL_DBG dbx
  65. # define BOOST_TEST_GUI_DBG dbx-ddd
  66. # include <procfs.h>
  67. # elif defined(linux) || defined(__linux)
  68. # define BOOST_LINUX_BASED_DEBUG
  69. # include <sys/ptrace.h>
  70. # ifndef BOOST_TEST_STAT_LINE_MAX
  71. # define BOOST_TEST_STAT_LINE_MAX 500
  72. # endif
  73. # ifndef BOOST_TEST_DBG_LIST
  74. # define BOOST_TEST_DBG_LIST gdb
  75. # endif
  76. # define BOOST_TEST_CNL_DBG gdb
  77. # define BOOST_TEST_GUI_DBG gdb-xterm
  78. # endif
  79. #endif
  80. #include <boost/test/detail/suppress_warnings.hpp>
  81. //____________________________________________________________________________//
  82. namespace boost {
  83. namespace debug {
  84. using unit_test::const_string;
  85. // ************************************************************************** //
  86. // ************** debug::info_t ************** //
  87. // ************************************************************************** //
  88. namespace {
  89. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  90. template<typename T>
  91. inline void
  92. dyn_symbol( T& res, char const* module_name, char const* symbol_name )
  93. {
  94. HMODULE m = ::GetModuleHandleA( module_name );
  95. if( !m )
  96. m = ::LoadLibraryA( module_name );
  97. res = reinterpret_cast<T>( ::GetProcAddress( m, symbol_name ) );
  98. }
  99. //____________________________________________________________________________//
  100. static struct info_t {
  101. typedef BOOL (WINAPI* IsDebuggerPresentT)();
  102. typedef LONG (WINAPI* RegQueryValueExT)( HKEY, char const* /*LPTSTR*/, LPDWORD, LPDWORD, LPBYTE, LPDWORD );
  103. typedef LONG (WINAPI* RegOpenKeyT)( HKEY, char const* /*LPCTSTR*/, PHKEY );
  104. typedef LONG (WINAPI* RegCloseKeyT)( HKEY );
  105. info_t();
  106. IsDebuggerPresentT m_is_debugger_present;
  107. RegOpenKeyT m_reg_open_key;
  108. RegQueryValueExT m_reg_query_value;
  109. RegCloseKeyT m_reg_close_key;
  110. } s_info;
  111. //____________________________________________________________________________//
  112. info_t::info_t()
  113. {
  114. dyn_symbol( m_is_debugger_present, "kernel32", "IsDebuggerPresent" );
  115. dyn_symbol( m_reg_open_key, "advapi32", "RegOpenKeyA" );
  116. dyn_symbol( m_reg_query_value, "advapi32", "RegQueryValueExA" );
  117. dyn_symbol( m_reg_close_key, "advapi32", "RegCloseKey" );
  118. }
  119. //____________________________________________________________________________//
  120. #elif defined(BOOST_UNIX_BASED_DEBUG)
  121. // ************************************************************************** //
  122. // ************** fd_holder ************** //
  123. // ************************************************************************** //
  124. struct fd_holder {
  125. explicit fd_holder( int fd ) : m_fd( fd ) {}
  126. ~fd_holder()
  127. {
  128. if( m_fd != -1 )
  129. ::close( m_fd );
  130. }
  131. operator int() { return m_fd; }
  132. private:
  133. // Data members
  134. int m_fd;
  135. };
  136. // ************************************************************************** //
  137. // ************** process_info ************** //
  138. // ************************************************************************** //
  139. struct process_info {
  140. // Constructor
  141. explicit process_info( int pid );
  142. // access methods
  143. int parent_pid() const { return m_parent_pid; }
  144. const_string binary_name() const { return m_binary_name; }
  145. const_string binary_path() const { return m_binary_path; }
  146. private:
  147. // Data members
  148. int m_parent_pid;
  149. const_string m_binary_name;
  150. const_string m_binary_path;
  151. #if defined(BOOST_SUN_BASED_DEBUG)
  152. struct psinfo m_psi;
  153. #elif defined(BOOST_LINUX_BASED_DEBUG)
  154. char m_stat_line[BOOST_TEST_STAT_LINE_MAX+1];
  155. #endif
  156. char m_binary_path_buff[500+1]; // !! ??
  157. };
  158. //____________________________________________________________________________//
  159. process_info::process_info( int pid )
  160. : m_parent_pid( 0 )
  161. {
  162. #if defined(BOOST_SUN_BASED_DEBUG)
  163. char fname_buff[30];
  164. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/psinfo", pid );
  165. fd_holder psinfo_fd( ::open( fname_buff, O_RDONLY ) );
  166. if( psinfo_fd == -1 )
  167. return;
  168. if( ::read( psinfo_fd, &m_psi, sizeof(m_psi) ) == -1 )
  169. return;
  170. m_parent_pid = m_psi.pr_ppid;
  171. m_binary_name.assign( m_psi.pr_fname );
  172. //-------------------------- //
  173. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/as", pid );
  174. fd_holder as_fd( ::open( fname_buff, O_RDONLY ) );
  175. uintptr_t binary_name_pos;
  176. // !! ?? could we avoid reading whole m_binary_path_buff?
  177. if( as_fd == -1 ||
  178. ::lseek( as_fd, m_psi.pr_argv, SEEK_SET ) == -1 ||
  179. ::read ( as_fd, &binary_name_pos, sizeof(binary_name_pos) ) == -1 ||
  180. ::lseek( as_fd, binary_name_pos, SEEK_SET ) == -1 ||
  181. ::read ( as_fd, m_binary_path_buff, sizeof(m_binary_path_buff) ) == -1 )
  182. return;
  183. m_binary_path.assign( m_binary_path_buff );
  184. #elif defined(BOOST_LINUX_BASED_DEBUG)
  185. char fname_buff[30];
  186. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/stat", pid );
  187. fd_holder psinfo_fd( ::open( fname_buff, O_RDONLY ) );
  188. if( psinfo_fd == -1 )
  189. return;
  190. ssize_t num_read = ::read( psinfo_fd, m_stat_line, sizeof(m_stat_line)-1 );
  191. if( num_read == -1 )
  192. return;
  193. m_stat_line[num_read] = 0;
  194. char const* name_beg = m_stat_line;
  195. while( *name_beg && *name_beg != '(' )
  196. ++name_beg;
  197. char const* name_end = name_beg+1;
  198. while( *name_end && *name_end != ')' )
  199. ++name_end;
  200. std::sscanf( name_end+1, "%*s%d", &m_parent_pid );
  201. m_binary_name.assign( name_beg+1, name_end );
  202. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/exe", pid );
  203. num_read = ::readlink( fname_buff, m_binary_path_buff, sizeof(m_binary_path_buff)-1 );
  204. if( num_read == -1 )
  205. return;
  206. m_binary_path_buff[num_read] = 0;
  207. m_binary_path.assign( m_binary_path_buff, num_read );
  208. #endif
  209. }
  210. //____________________________________________________________________________//
  211. // ************************************************************************** //
  212. // ************** prepare_window_title ************** //
  213. // ************************************************************************** //
  214. static char*
  215. prepare_window_title( dbg_startup_info const& dsi )
  216. {
  217. typedef unit_test::const_string str_t;
  218. static char title_str[50];
  219. str_t path_sep( "\\/" );
  220. str_t::iterator it = unit_test::find_last_of( dsi.binary_path.begin(), dsi.binary_path.end(),
  221. path_sep.begin(), path_sep.end() );
  222. if( it == dsi.binary_path.end() )
  223. it = dsi.binary_path.begin();
  224. else
  225. ++it;
  226. ::snprintf( title_str, sizeof(title_str), "%*s %ld", (int)(dsi.binary_path.end()-it), it, dsi.pid );
  227. return title_str;
  228. }
  229. //____________________________________________________________________________//
  230. // ************************************************************************** //
  231. // ************** save_execlp ************** //
  232. // ************************************************************************** //
  233. typedef unit_test::basic_cstring<char> mbuffer;
  234. inline char*
  235. copy_arg( mbuffer& dest, const_string arg )
  236. {
  237. if( dest.size() < arg.size()+1 )
  238. return 0;
  239. char* res = dest.begin();
  240. std::memcpy( res, arg.begin(), arg.size()+1 );
  241. dest.trim_left( arg.size()+1 );
  242. return res;
  243. }
  244. //____________________________________________________________________________//
  245. bool
  246. safe_execlp( char const* file, ... )
  247. {
  248. static char* argv_buff[200];
  249. va_list args;
  250. char const* arg;
  251. // first calculate actual number of arguments
  252. int num_args = 2; // file name and 0 at least
  253. va_start( args, file );
  254. while( !!(arg = va_arg( args, char const* )) )
  255. num_args++;
  256. va_end( args );
  257. // reserve space for the argument pointers array
  258. char** argv_it = argv_buff;
  259. mbuffer work_buff( reinterpret_cast<char*>(argv_buff), sizeof(argv_buff) );
  260. work_buff.trim_left( num_args * sizeof(char*) );
  261. // copy all the argument values into local storage
  262. if( !(*argv_it++ = copy_arg( work_buff, file )) )
  263. return false;
  264. printf( "!! %s\n", file );
  265. va_start( args, file );
  266. while( !!(arg = va_arg( args, char const* )) ) {
  267. printf( "!! %s\n", arg );
  268. if( !(*argv_it++ = copy_arg( work_buff, arg )) )
  269. return false;
  270. }
  271. va_end( args );
  272. *argv_it = 0;
  273. return ::execvp( file, argv_buff ) != -1;
  274. }
  275. //____________________________________________________________________________//
  276. // ************************************************************************** //
  277. // ************** start_debugger_in_emacs ************** //
  278. // ************************************************************************** //
  279. static void
  280. start_debugger_in_emacs( dbg_startup_info const& dsi, char const* emacs_name, char const* dbg_command )
  281. {
  282. char const* title = prepare_window_title( dsi );
  283. if( !title )
  284. return;
  285. dsi.display.is_empty()
  286. ? safe_execlp( emacs_name, "-title", title, "--eval", dbg_command, 0 )
  287. : safe_execlp( emacs_name, "-title", title, "-display", dsi.display.begin(), "--eval", dbg_command, 0 );
  288. }
  289. //____________________________________________________________________________//
  290. // ************************************************************************** //
  291. // ************** gdb starters ************** //
  292. // ************************************************************************** //
  293. static char const*
  294. prepare_gdb_cmnd_file( dbg_startup_info const& dsi )
  295. {
  296. // prepare pid value
  297. char pid_buff[16];
  298. ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
  299. unit_test::const_string pid_str( pid_buff );
  300. static char cmd_file_name[] = "/tmp/btl_gdb_cmd_XXXXXX"; // !! ??
  301. // prepare commands
  302. fd_holder cmd_fd( ::mkstemp( cmd_file_name ) );
  303. if( cmd_fd == -1 )
  304. return 0;
  305. #define WRITE_STR( str ) if( ::write( cmd_fd, str.begin(), str.size() ) == -1 ) return 0;
  306. #define WRITE_CSTR( str ) if( ::write( cmd_fd, str, sizeof( str )-1 ) == -1 ) return 0;
  307. WRITE_CSTR( "file " );
  308. WRITE_STR( dsi.binary_path );
  309. WRITE_CSTR( "\nattach " );
  310. WRITE_STR( pid_str );
  311. WRITE_CSTR( "\nshell unlink " );
  312. WRITE_STR( dsi.init_done_lock );
  313. WRITE_CSTR( "\ncont" );
  314. if( dsi.break_or_continue )
  315. WRITE_CSTR( "\nup 4" );
  316. WRITE_CSTR( "\necho \\n" ); // !! ??
  317. WRITE_CSTR( "\nlist -" );
  318. WRITE_CSTR( "\nlist" );
  319. WRITE_CSTR( "\nshell unlink " );
  320. WRITE_CSTR( cmd_file_name );
  321. return cmd_file_name;
  322. }
  323. //____________________________________________________________________________//
  324. static void
  325. start_gdb_in_console( dbg_startup_info const& dsi )
  326. {
  327. char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
  328. if( !cmnd_file_name )
  329. return;
  330. safe_execlp( "gdb", "-q", "-x", cmnd_file_name, 0 );
  331. }
  332. //____________________________________________________________________________//
  333. static void
  334. start_gdb_in_xterm( dbg_startup_info const& dsi )
  335. {
  336. char const* title = prepare_window_title( dsi );
  337. char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
  338. if( !title || !cmnd_file_name )
  339. return;
  340. safe_execlp( "xterm", "-T", title, "-display", dsi.display.begin(),
  341. "-bg", "black", "-fg", "white", "-geometry", "88x30+10+10", "-fn", "9x15", "-e",
  342. "gdb", "-q", "-x", cmnd_file_name, 0 );
  343. }
  344. //____________________________________________________________________________//
  345. static void
  346. start_gdb_in_emacs( dbg_startup_info const& dsi )
  347. {
  348. char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
  349. if( !cmnd_file_name )
  350. return;
  351. char dbg_cmd_buff[500]; // !! ??
  352. ::snprintf( dbg_cmd_buff, sizeof(dbg_cmd_buff), "(progn (gdb \"gdb -q -x %s\"))", cmnd_file_name );
  353. start_debugger_in_emacs( dsi, "emacs", dbg_cmd_buff );
  354. }
  355. //____________________________________________________________________________//
  356. static void
  357. start_gdb_in_xemacs( dbg_startup_info const& )
  358. {
  359. // !! ??
  360. }
  361. //____________________________________________________________________________//
  362. // ************************************************************************** //
  363. // ************** dbx starters ************** //
  364. // ************************************************************************** //
  365. static char const*
  366. prepare_dbx_cmd_line( dbg_startup_info const& dsi, bool list_source = true )
  367. {
  368. static char cmd_line_buff[500]; // !! ??
  369. ::snprintf( cmd_line_buff, sizeof(cmd_line_buff), "unlink %s;cont;%s%s",
  370. dsi.init_done_lock.begin(),
  371. dsi.break_or_continue ? "up 2;": "",
  372. list_source ? "echo \" \";list -w3;" : "" );
  373. return cmd_line_buff;
  374. }
  375. //____________________________________________________________________________//
  376. static void
  377. start_dbx_in_console( dbg_startup_info const& dsi )
  378. {
  379. char pid_buff[16];
  380. ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
  381. safe_execlp( "dbx", "-q", "-c", prepare_dbx_cmd_line( dsi ), dsi.binary_path.begin(), pid_buff, 0 );
  382. }
  383. //____________________________________________________________________________//
  384. static void
  385. start_dbx_in_xterm( dbg_startup_info const& dsi )
  386. {
  387. char const* title = prepare_window_title( dsi );
  388. if( !title )
  389. return;
  390. char pid_buff[16]; // !! ??
  391. ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
  392. safe_execlp( "xterm", "-T", title, "-display", dsi.display.begin(),
  393. "-bg", "black", "-fg", "white", "-geometry", "88x30+10+10", "-fn", "9x15", "-e",
  394. "dbx", "-q", "-c", prepare_dbx_cmd_line( dsi ), dsi.binary_path.begin(), pid_buff, 0 );
  395. }
  396. //____________________________________________________________________________//
  397. static void
  398. start_dbx_in_emacs( dbg_startup_info const& /*dsi*/ )
  399. {
  400. // char dbg_cmd_buff[500]; // !! ??
  401. //
  402. // ::snprintf( dbg_cmd_buff, sizeof(dbg_cmd_buff), "(progn (dbx \"dbx -q -c cont %s %ld\"))", dsi.binary_path.begin(), dsi.pid );
  403. // start_debugger_in_emacs( dsi, "emacs", dbg_cmd_buff );
  404. }
  405. //____________________________________________________________________________//
  406. static void
  407. start_dbx_in_xemacs( dbg_startup_info const& )
  408. {
  409. // !! ??
  410. }
  411. //____________________________________________________________________________//
  412. static void
  413. start_dbx_in_ddd( dbg_startup_info const& dsi )
  414. {
  415. char const* title = prepare_window_title( dsi );
  416. if( !title )
  417. return;
  418. char pid_buff[16]; // !! ??
  419. ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
  420. safe_execlp( "ddd", "-display", dsi.display.begin(),
  421. "--dbx", "-q", "-c", prepare_dbx_cmd_line( dsi, false ), dsi.binary_path.begin(), pid_buff, 0 );
  422. }
  423. //____________________________________________________________________________//
  424. // ************************************************************************** //
  425. // ************** debug::info_t ************** //
  426. // ************************************************************************** //
  427. static struct info_t {
  428. // Constructor
  429. info_t();
  430. // Public properties
  431. unit_test::readwrite_property<std::string> p_dbg;
  432. // Data members
  433. std::map<std::string,dbg_starter> m_dbg_starter_reg;
  434. } s_info;
  435. //____________________________________________________________________________//
  436. info_t::info_t()
  437. {
  438. p_dbg.value = ::getenv( "DISPLAY" )
  439. ? std::string( BOOST_STRINGIZE( BOOST_TEST_GUI_DBG ) )
  440. : std::string( BOOST_STRINGIZE( BOOST_TEST_CNL_DBG ) );
  441. m_dbg_starter_reg[std::string("gdb")] = &start_gdb_in_console;
  442. m_dbg_starter_reg[std::string("gdb-emacs")] = &start_gdb_in_emacs;
  443. m_dbg_starter_reg[std::string("gdb-xterm")] = &start_gdb_in_xterm;
  444. m_dbg_starter_reg[std::string("gdb-xemacs")] = &start_gdb_in_xemacs;
  445. m_dbg_starter_reg[std::string("dbx")] = &start_dbx_in_console;
  446. m_dbg_starter_reg[std::string("dbx-emacs")] = &start_dbx_in_emacs;
  447. m_dbg_starter_reg[std::string("dbx-xterm")] = &start_dbx_in_xterm;
  448. m_dbg_starter_reg[std::string("dbx-xemacs")] = &start_dbx_in_xemacs;
  449. m_dbg_starter_reg[std::string("dbx-ddd")] = &start_dbx_in_ddd;
  450. }
  451. //____________________________________________________________________________//
  452. #endif
  453. } // local namespace
  454. // ************************************************************************** //
  455. // ************** check if program is running under debugger ************** //
  456. // ************************************************************************** //
  457. bool
  458. under_debugger()
  459. {
  460. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  461. return !!s_info.m_is_debugger_present && s_info.m_is_debugger_present();
  462. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  463. // !! ?? could/should we cache the result somehow?
  464. const_string dbg_list = BOOST_TEST_STRINGIZE( BOOST_TEST_DBG_LIST );
  465. pid_t pid = ::getpid();
  466. while( pid != 0 ) {
  467. process_info pi( pid );
  468. // !! ?? should we use tokenizer here instead?
  469. if( dbg_list.find( pi.binary_name() ) != const_string::npos )
  470. return true;
  471. pid = (pi.parent_pid() == pid ? 0 : pi.parent_pid());
  472. }
  473. return false;
  474. #else // ****************************************************** default
  475. return false;
  476. #endif
  477. }
  478. //____________________________________________________________________________//
  479. // ************************************************************************** //
  480. // ************** cause program to break execution ************** //
  481. // ************** in debugger at call point ************** //
  482. // ************************************************************************** //
  483. void
  484. debugger_break()
  485. {
  486. // !! ?? auto-start debugger?
  487. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  488. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1300) || \
  489. BOOST_WORKAROUND(__GNUC__, >= 3) && !defined(__MINGW32__) || \
  490. defined(__INTEL_COMPILER)
  491. # define BOOST_DEBUG_BREAK __debugbreak
  492. #else
  493. # define BOOST_DEBUG_BREAK DebugBreak
  494. #endif
  495. #ifndef __MINGW32__
  496. if( !under_debugger() ) {
  497. __try {
  498. __try {
  499. BOOST_DEBUG_BREAK();
  500. }
  501. __except( UnhandledExceptionFilter(GetExceptionInformation()) )
  502. {
  503. // User opted to ignore the breakpoint
  504. return;
  505. }
  506. }
  507. __except (EXCEPTION_EXECUTE_HANDLER)
  508. {
  509. // If we got here, the user has pushed Debug. Debugger is already attached to our process and we
  510. // continue to let the another BOOST_DEBUG_BREAK to be called.
  511. }
  512. }
  513. #endif
  514. BOOST_DEBUG_BREAK();
  515. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  516. ::kill( ::getpid(), SIGTRAP );
  517. #else // ****************************************************** default
  518. #endif
  519. }
  520. //____________________________________________________________________________//
  521. // ************************************************************************** //
  522. // ************** console debugger setup ************** //
  523. // ************************************************************************** //
  524. #if defined(BOOST_UNIX_BASED_DEBUG) // ************************ UNIX
  525. std::string
  526. set_debugger( unit_test::const_string dbg_id, dbg_starter s )
  527. {
  528. std::string old = s_info.p_dbg;
  529. assign_op( s_info.p_dbg.value, dbg_id, 0 );
  530. if( !!s )
  531. s_info.m_dbg_starter_reg[s_info.p_dbg] = s;
  532. return old;
  533. }
  534. #else // ***************************************************** default
  535. std::string
  536. set_debugger( unit_test::const_string, dbg_starter )
  537. {
  538. return std::string();
  539. }
  540. #endif
  541. //____________________________________________________________________________//
  542. // ************************************************************************** //
  543. // ************** attach debugger to the current process ************** //
  544. // ************************************************************************** //
  545. bool
  546. attach_debugger( bool break_or_continue )
  547. {
  548. if( under_debugger() )
  549. return false;
  550. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  551. const int MAX_CMD_LINE = 200;
  552. // *************************************************** //
  553. // Debugger "ready" event
  554. SECURITY_ATTRIBUTES attr;
  555. attr.nLength = sizeof(attr);
  556. attr.lpSecurityDescriptor = NULL;
  557. attr.bInheritHandle = true;
  558. // manual resettable, initially non signaled, unnamed event,
  559. // that will signal me that debugger initialization is done
  560. HANDLE dbg_init_done_ev = ::CreateEvent(
  561. &attr, // pointer to security attributes
  562. true, // flag for manual-reset event
  563. false, // flag for initial state
  564. NULL // pointer to event-object name
  565. );
  566. if( !dbg_init_done_ev )
  567. return false;
  568. // *************************************************** //
  569. // Debugger command line format
  570. HKEY reg_key;
  571. if( !s_info.m_reg_open_key || (*s_info.m_reg_open_key)(
  572. HKEY_LOCAL_MACHINE, // handle of open key
  573. "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", // name of subkey to open
  574. &reg_key ) != ERROR_SUCCESS ) // address of handle of open key
  575. return false;
  576. char format[MAX_CMD_LINE];
  577. DWORD format_size = MAX_CMD_LINE;
  578. DWORD type = REG_SZ;
  579. if( !s_info.m_reg_query_value || (*s_info.m_reg_query_value)(
  580. reg_key, // handle of open key
  581. "Debugger", // name of subkey to query
  582. 0, // reserved
  583. &type, // value type
  584. (LPBYTE)format, // buffer for returned string
  585. &format_size ) != ERROR_SUCCESS ) // in: buffer size; out: actual size of returned string
  586. return false;
  587. if( !s_info.m_reg_close_key || (*s_info.m_reg_close_key)( reg_key ) != ERROR_SUCCESS )
  588. return false;
  589. // *************************************************** //
  590. // Debugger command line
  591. char cmd_line[MAX_CMD_LINE];
  592. std::sprintf( cmd_line, format, ::GetCurrentProcessId(), dbg_init_done_ev );
  593. // *************************************************** //
  594. // Debugger window parameters
  595. STARTUPINFOA startup_info;
  596. std::memset( &startup_info, 0, sizeof(startup_info) );
  597. startup_info.cb = sizeof(startup_info);
  598. startup_info.dwFlags = STARTF_USESHOWWINDOW;
  599. startup_info.wShowWindow = SW_SHOWNORMAL;
  600. // debugger process s_info
  601. PROCESS_INFORMATION debugger_info;
  602. bool created = !!::CreateProcessA(
  603. NULL, // pointer to name of executable module; NULL - use the one in command line
  604. cmd_line, // pointer to command line string
  605. NULL, // pointer to process security attributes; NULL - debugger's handle can't be inherited
  606. NULL, // pointer to thread security attributes; NULL - debugger's handle can't be inherited
  607. true, // debugger inherit opened handles
  608. 0, // priority flags; 0 - normal priority
  609. NULL, // pointer to new environment block; NULL - use this process environment
  610. NULL, // pointer to current directory name; NULL - use this process correct directory
  611. &startup_info, // pointer to STARTUPINFO that specifies main window appearance
  612. &debugger_info // pointer to PROCESS_INFORMATION that will contain the new process identification
  613. );
  614. if( created )
  615. ::WaitForSingleObject( dbg_init_done_ev, INFINITE );
  616. ::CloseHandle( dbg_init_done_ev );
  617. if( !created )
  618. return false;
  619. if( break_or_continue )
  620. debugger_break();
  621. return true;
  622. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  623. char init_done_lock_fn[] = "/tmp/btl_dbg_init_done_XXXXXX";
  624. fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
  625. if( init_done_lock_fd == -1 )
  626. return false;
  627. pid_t child_pid = fork();
  628. if( child_pid == -1 )
  629. return false;
  630. if( child_pid != 0 ) { // parent process - here we will start the debugger
  631. dbg_startup_info dsi;
  632. process_info pi( child_pid );
  633. if( pi.binary_path().is_empty() )
  634. ::exit( -1 );
  635. dsi.pid = child_pid;
  636. dsi.break_or_continue = break_or_continue;
  637. dsi.binary_path = pi.binary_path();
  638. dsi.display = ::getenv( "DISPLAY" );
  639. dsi.init_done_lock = init_done_lock_fn;
  640. dbg_starter starter = s_info.m_dbg_starter_reg[s_info.p_dbg];
  641. if( !!starter )
  642. starter( dsi );
  643. ::perror( "Boost.Test execution monitor failed to start a debugger:" );
  644. ::exit( -1 );
  645. }
  646. // child process - here we will continue our test module execution ; // !! ?? should it be vice versa
  647. while( ::access( init_done_lock_fn, F_OK ) == 0 ) {
  648. struct timeval to = { 0, 100 };
  649. ::select( 0, 0, 0, 0, &to );
  650. }
  651. // char dummy;
  652. // while( ::read( init_done_lock_fd, &dummy, sizeof(char) ) == 0 );
  653. if( break_or_continue )
  654. debugger_break();
  655. return true;
  656. #else // ****************************************************** default
  657. return false;
  658. #endif
  659. }
  660. //____________________________________________________________________________//
  661. // ************************************************************************** //
  662. // ************** switch on/off detect memory leaks feature ************** //
  663. // ************************************************************************** //
  664. void
  665. detect_memory_leaks( bool on_off )
  666. {
  667. unit_test::ut_detail::ignore_unused_variable_warning( on_off );
  668. #ifdef BOOST_MS_CRT_BASED_DEBUG
  669. int flags = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
  670. if( !on_off )
  671. flags &= ~_CRTDBG_LEAK_CHECK_DF;
  672. else {
  673. flags |= _CRTDBG_LEAK_CHECK_DF;
  674. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  675. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
  676. }
  677. _CrtSetDbgFlag ( flags );
  678. #endif // BOOST_MS_CRT_BASED_DEBUG
  679. }
  680. //____________________________________________________________________________//
  681. // ************************************************************************** //
  682. // ************** cause program to break execution in ************** //
  683. // ************** debugger at specific allocation point ************** //
  684. // ************************************************************************** //
  685. void
  686. break_memory_alloc( long mem_alloc_order_num )
  687. {
  688. unit_test::ut_detail::ignore_unused_variable_warning( mem_alloc_order_num );
  689. #ifdef BOOST_MS_CRT_BASED_DEBUG
  690. _CrtSetBreakAlloc( mem_alloc_order_num );
  691. #endif // BOOST_MS_CRT_BASED_DEBUG
  692. }
  693. } // namespace debug
  694. } // namespace boost
  695. //____________________________________________________________________________//
  696. #include <boost/test/detail/enable_warnings.hpp>
  697. #endif // BOOST_TEST_DEBUG_API_IPP_112006GER