debug.ipp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  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/global_typedef.hpp>
  18. #include <boost/test/debug.hpp>
  19. #include <boost/test/debug_config.hpp>
  20. #include <boost/core/ignore_unused.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. # ifdef BOOST_NO_STDC_NAMESPACE
  34. namespace std { using ::memset; using ::sprintf; }
  35. # endif
  36. #elif defined(unix) || defined(__unix) // ********************* UNIX
  37. # define BOOST_UNIX_BASED_DEBUG
  38. // Boost.Test
  39. #include <boost/test/utils/class_properties.hpp>
  40. #include <boost/test/utils/algorithm.hpp>
  41. // STL
  42. #include <cstring> // std::memcpy
  43. #include <map>
  44. #include <cstdio>
  45. #include <stdarg.h> // !! ?? cstdarg
  46. // SYSTEM API
  47. # include <unistd.h>
  48. # include <signal.h>
  49. # include <fcntl.h>
  50. # include <sys/types.h>
  51. # include <sys/stat.h>
  52. # include <sys/wait.h>
  53. # include <sys/time.h>
  54. # include <stdio.h>
  55. # include <stdlib.h>
  56. # if defined(sun) || defined(__sun)
  57. # define BOOST_SUN_BASED_DEBUG
  58. # ifndef BOOST_TEST_DBG_LIST
  59. # define BOOST_TEST_DBG_LIST dbx;gdb
  60. # endif
  61. # define BOOST_TEST_CNL_DBG dbx
  62. # define BOOST_TEST_GUI_DBG dbx-ddd
  63. # include <procfs.h>
  64. # elif defined(linux) || defined(__linux)
  65. # define BOOST_LINUX_BASED_DEBUG
  66. # include <sys/ptrace.h>
  67. # ifndef BOOST_TEST_STAT_LINE_MAX
  68. # define BOOST_TEST_STAT_LINE_MAX 500
  69. # endif
  70. # ifndef BOOST_TEST_DBG_LIST
  71. # define BOOST_TEST_DBG_LIST gdb
  72. # endif
  73. # define BOOST_TEST_CNL_DBG gdb
  74. # define BOOST_TEST_GUI_DBG gdb-xterm
  75. # endif
  76. #endif
  77. #include <boost/test/detail/suppress_warnings.hpp>
  78. //____________________________________________________________________________//
  79. namespace boost {
  80. namespace debug {
  81. using unit_test::const_string;
  82. // ************************************************************************** //
  83. // ************** debug::info_t ************** //
  84. // ************************************************************************** //
  85. namespace {
  86. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  87. template<typename T>
  88. inline void
  89. dyn_symbol( T& res, char const* module_name, char const* symbol_name )
  90. {
  91. HMODULE m = ::GetModuleHandleA( module_name );
  92. if( !m )
  93. m = ::LoadLibraryA( module_name );
  94. res = reinterpret_cast<T>( ::GetProcAddress( m, symbol_name ) );
  95. }
  96. //____________________________________________________________________________//
  97. static struct info_t {
  98. typedef BOOL (WINAPI* IsDebuggerPresentT)();
  99. typedef LONG (WINAPI* RegQueryValueExT)( HKEY, char const* /*LPTSTR*/, LPDWORD, LPDWORD, LPBYTE, LPDWORD );
  100. typedef LONG (WINAPI* RegOpenKeyT)( HKEY, char const* /*LPCTSTR*/, PHKEY );
  101. typedef LONG (WINAPI* RegCloseKeyT)( HKEY );
  102. info_t();
  103. IsDebuggerPresentT m_is_debugger_present;
  104. RegOpenKeyT m_reg_open_key;
  105. RegQueryValueExT m_reg_query_value;
  106. RegCloseKeyT m_reg_close_key;
  107. } s_info;
  108. //____________________________________________________________________________//
  109. info_t::info_t()
  110. {
  111. dyn_symbol( m_is_debugger_present, "kernel32", "IsDebuggerPresent" );
  112. dyn_symbol( m_reg_open_key, "advapi32", "RegOpenKeyA" );
  113. dyn_symbol( m_reg_query_value, "advapi32", "RegQueryValueExA" );
  114. dyn_symbol( m_reg_close_key, "advapi32", "RegCloseKey" );
  115. }
  116. //____________________________________________________________________________//
  117. #elif defined(BOOST_UNIX_BASED_DEBUG)
  118. // ************************************************************************** //
  119. // ************** fd_holder ************** //
  120. // ************************************************************************** //
  121. struct fd_holder {
  122. explicit fd_holder( int fd ) : m_fd( fd ) {}
  123. ~fd_holder()
  124. {
  125. if( m_fd != -1 )
  126. ::close( m_fd );
  127. }
  128. operator int() { return m_fd; }
  129. private:
  130. // Data members
  131. int m_fd;
  132. };
  133. // ************************************************************************** //
  134. // ************** process_info ************** //
  135. // ************************************************************************** //
  136. struct process_info {
  137. // Constructor
  138. explicit process_info( int pid );
  139. // access methods
  140. int parent_pid() const { return m_parent_pid; }
  141. const_string binary_name() const { return m_binary_name; }
  142. const_string binary_path() const { return m_binary_path; }
  143. private:
  144. // Data members
  145. int m_parent_pid;
  146. const_string m_binary_name;
  147. const_string m_binary_path;
  148. #if defined(BOOST_SUN_BASED_DEBUG)
  149. struct psinfo m_psi;
  150. char m_binary_path_buff[500+1]; // !! ??
  151. #elif defined(BOOST_LINUX_BASED_DEBUG)
  152. char m_stat_line[BOOST_TEST_STAT_LINE_MAX+1];
  153. char m_binary_path_buff[500+1]; // !! ??
  154. #endif
  155. };
  156. //____________________________________________________________________________//
  157. process_info::process_info( int pid )
  158. : m_parent_pid( 0 )
  159. {
  160. #if defined(BOOST_SUN_BASED_DEBUG)
  161. char fname_buff[30];
  162. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/psinfo", pid );
  163. fd_holder psinfo_fd( ::open( fname_buff, O_RDONLY ) );
  164. if( psinfo_fd == -1 )
  165. return;
  166. if( ::read( psinfo_fd, &m_psi, sizeof(m_psi) ) == -1 )
  167. return;
  168. m_parent_pid = m_psi.pr_ppid;
  169. m_binary_name.assign( m_psi.pr_fname );
  170. //-------------------------- //
  171. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/as", pid );
  172. fd_holder as_fd( ::open( fname_buff, O_RDONLY ) );
  173. uintptr_t binary_name_pos;
  174. // !! ?? could we avoid reading whole m_binary_path_buff?
  175. if( as_fd == -1 ||
  176. ::lseek( as_fd, m_psi.pr_argv, SEEK_SET ) == -1 ||
  177. ::read ( as_fd, &binary_name_pos, sizeof(binary_name_pos) ) == -1 ||
  178. ::lseek( as_fd, binary_name_pos, SEEK_SET ) == -1 ||
  179. ::read ( as_fd, m_binary_path_buff, sizeof(m_binary_path_buff) ) == -1 )
  180. return;
  181. m_binary_path.assign( m_binary_path_buff );
  182. #elif defined(BOOST_LINUX_BASED_DEBUG)
  183. char fname_buff[30];
  184. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/stat", pid );
  185. fd_holder psinfo_fd( ::open( fname_buff, O_RDONLY ) );
  186. if( psinfo_fd == -1 )
  187. return;
  188. ssize_t num_read = ::read( psinfo_fd, m_stat_line, sizeof(m_stat_line)-1 );
  189. if( num_read == -1 )
  190. return;
  191. m_stat_line[num_read] = 0;
  192. char const* name_beg = m_stat_line;
  193. while( *name_beg && *name_beg != '(' )
  194. ++name_beg;
  195. char const* name_end = name_beg+1;
  196. while( *name_end && *name_end != ')' )
  197. ++name_end;
  198. std::sscanf( name_end+1, "%*s%d", &m_parent_pid );
  199. m_binary_name.assign( name_beg+1, name_end );
  200. ::snprintf( fname_buff, sizeof(fname_buff), "/proc/%d/exe", pid );
  201. num_read = ::readlink( fname_buff, m_binary_path_buff, sizeof(m_binary_path_buff)-1 );
  202. if( num_read == -1 )
  203. return;
  204. m_binary_path_buff[num_read] = 0;
  205. m_binary_path.assign( m_binary_path_buff, num_read );
  206. #endif
  207. }
  208. //____________________________________________________________________________//
  209. // ************************************************************************** //
  210. // ************** prepare_window_title ************** //
  211. // ************************************************************************** //
  212. static char*
  213. prepare_window_title( dbg_startup_info const& dsi )
  214. {
  215. typedef unit_test::const_string str_t;
  216. static char title_str[50];
  217. str_t path_sep( "\\/" );
  218. str_t::iterator it = unit_test::utils::find_last_of( dsi.binary_path.begin(), dsi.binary_path.end(),
  219. path_sep.begin(), path_sep.end() );
  220. if( it == dsi.binary_path.end() )
  221. it = dsi.binary_path.begin();
  222. else
  223. ++it;
  224. ::snprintf( title_str, sizeof(title_str), "%*s %ld", (int)(dsi.binary_path.end()-it), it, dsi.pid );
  225. return title_str;
  226. }
  227. //____________________________________________________________________________//
  228. // ************************************************************************** //
  229. // ************** save_execlp ************** //
  230. // ************************************************************************** //
  231. typedef unit_test::basic_cstring<char> mbuffer;
  232. inline char*
  233. copy_arg( mbuffer& dest, const_string arg )
  234. {
  235. if( dest.size() < arg.size()+1 )
  236. return 0;
  237. char* res = dest.begin();
  238. std::memcpy( res, arg.begin(), arg.size()+1 );
  239. dest.trim_left( arg.size()+1 );
  240. return res;
  241. }
  242. //____________________________________________________________________________//
  243. bool
  244. safe_execlp( char const* file, ... )
  245. {
  246. static char* argv_buff[200];
  247. va_list args;
  248. char const* arg;
  249. // first calculate actual number of arguments
  250. int num_args = 2; // file name and 0 at least
  251. va_start( args, file );
  252. while( !!(arg = va_arg( args, char const* )) )
  253. num_args++;
  254. va_end( args );
  255. // reserve space for the argument pointers array
  256. char** argv_it = argv_buff;
  257. mbuffer work_buff( reinterpret_cast<char*>(argv_buff), sizeof(argv_buff) );
  258. work_buff.trim_left( num_args * sizeof(char*) );
  259. // copy all the argument values into local storage
  260. if( !(*argv_it++ = copy_arg( work_buff, file )) )
  261. return false;
  262. printf( "!! %s\n", file );
  263. va_start( args, file );
  264. while( !!(arg = va_arg( args, char const* )) ) {
  265. printf( "!! %s\n", arg );
  266. if( !(*argv_it++ = copy_arg( work_buff, arg )) ) {
  267. va_end( args );
  268. return false;
  269. }
  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 defined(__GNUC__) && !defined(__MINGW32__) || \
  489. defined(__INTEL_COMPILER)
  490. # define BOOST_DEBUG_BREAK __debugbreak
  491. #else
  492. # define BOOST_DEBUG_BREAK DebugBreak
  493. #endif
  494. #ifndef __MINGW32__
  495. if( !under_debugger() ) {
  496. __try {
  497. __try {
  498. BOOST_DEBUG_BREAK();
  499. }
  500. __except( UnhandledExceptionFilter(GetExceptionInformation()) )
  501. {
  502. // User opted to ignore the breakpoint
  503. return;
  504. }
  505. }
  506. __except (EXCEPTION_EXECUTE_HANDLER)
  507. {
  508. // If we got here, the user has pushed Debug. Debugger is already attached to our process and we
  509. // continue to let the another BOOST_DEBUG_BREAK to be called.
  510. }
  511. }
  512. #endif
  513. BOOST_DEBUG_BREAK();
  514. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  515. ::kill( ::getpid(), SIGTRAP );
  516. #else // ****************************************************** default
  517. #endif
  518. }
  519. //____________________________________________________________________________//
  520. // ************************************************************************** //
  521. // ************** console debugger setup ************** //
  522. // ************************************************************************** //
  523. #if defined(BOOST_UNIX_BASED_DEBUG) // ************************ UNIX
  524. std::string
  525. set_debugger( unit_test::const_string dbg_id, dbg_starter s )
  526. {
  527. std::string old = s_info.p_dbg;
  528. assign_op( s_info.p_dbg.value, dbg_id, 0 );
  529. if( !!s )
  530. s_info.m_dbg_starter_reg[s_info.p_dbg.get()] = s;
  531. return old;
  532. }
  533. #else // ***************************************************** default
  534. std::string
  535. set_debugger( unit_test::const_string, dbg_starter )
  536. {
  537. return std::string();
  538. }
  539. #endif
  540. //____________________________________________________________________________//
  541. // ************************************************************************** //
  542. // ************** attach debugger to the current process ************** //
  543. // ************************************************************************** //
  544. #if defined(BOOST_WIN32_BASED_DEBUG)
  545. struct safe_handle_helper
  546. {
  547. HANDLE& handle;
  548. safe_handle_helper(HANDLE &handle_) : handle(handle_) {}
  549. void close_handle()
  550. {
  551. if( handle != INVALID_HANDLE_VALUE )
  552. {
  553. ::CloseHandle( handle );
  554. handle = INVALID_HANDLE_VALUE;
  555. }
  556. }
  557. ~safe_handle_helper()
  558. {
  559. close_handle();
  560. }
  561. };
  562. #endif
  563. bool
  564. attach_debugger( bool break_or_continue )
  565. {
  566. if( under_debugger() )
  567. return false;
  568. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  569. const int MAX_CMD_LINE = 200;
  570. // *************************************************** //
  571. // Debugger "ready" event
  572. SECURITY_ATTRIBUTES attr;
  573. attr.nLength = sizeof(attr);
  574. attr.lpSecurityDescriptor = NULL;
  575. attr.bInheritHandle = true;
  576. // manual resettable, initially non signaled, unnamed event,
  577. // that will signal me that debugger initialization is done
  578. HANDLE dbg_init_done_ev = ::CreateEvent(
  579. &attr, // pointer to security attributes
  580. true, // flag for manual-reset event
  581. false, // flag for initial state
  582. NULL // pointer to event-object name
  583. );
  584. if( !dbg_init_done_ev )
  585. return false;
  586. safe_handle_helper safe_handle_obj( dbg_init_done_ev );
  587. // *************************************************** //
  588. // Debugger command line format
  589. HKEY reg_key;
  590. if( !s_info.m_reg_open_key || (*s_info.m_reg_open_key)(
  591. HKEY_LOCAL_MACHINE, // handle of open key
  592. "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", // name of subkey to open
  593. &reg_key ) != ERROR_SUCCESS ) // address of handle of open key
  594. return false;
  595. char format[MAX_CMD_LINE];
  596. DWORD format_size = MAX_CMD_LINE;
  597. DWORD type = REG_SZ;
  598. bool b_read_key = s_info.m_reg_query_value &&
  599. ((*s_info.m_reg_query_value)(
  600. reg_key, // handle of open key
  601. "Debugger", // name of subkey to query
  602. 0, // reserved
  603. &type, // value type
  604. (LPBYTE)format, // buffer for returned string
  605. &format_size ) == ERROR_SUCCESS ); // in: buffer size; out: actual size of returned string
  606. if( !s_info.m_reg_close_key || (*s_info.m_reg_close_key)( reg_key ) != ERROR_SUCCESS )
  607. return false;
  608. if( !b_read_key )
  609. return false;
  610. // *************************************************** //
  611. // Debugger command line
  612. char cmd_line[MAX_CMD_LINE];
  613. std::sprintf( cmd_line, format, ::GetCurrentProcessId(), dbg_init_done_ev );
  614. // *************************************************** //
  615. // Debugger window parameters
  616. STARTUPINFOA startup_info;
  617. std::memset( &startup_info, 0, sizeof(startup_info) );
  618. startup_info.cb = sizeof(startup_info);
  619. startup_info.dwFlags = STARTF_USESHOWWINDOW;
  620. startup_info.wShowWindow = SW_SHOWNORMAL;
  621. // debugger process s_info
  622. PROCESS_INFORMATION debugger_info;
  623. bool created = !!::CreateProcessA(
  624. NULL, // pointer to name of executable module; NULL - use the one in command line
  625. cmd_line, // pointer to command line string
  626. NULL, // pointer to process security attributes; NULL - debugger's handle can't be inherited
  627. NULL, // pointer to thread security attributes; NULL - debugger's handle can't be inherited
  628. true, // debugger inherit opened handles
  629. 0, // priority flags; 0 - normal priority
  630. NULL, // pointer to new environment block; NULL - use this process environment
  631. NULL, // pointer to current directory name; NULL - use this process correct directory
  632. &startup_info, // pointer to STARTUPINFO that specifies main window appearance
  633. &debugger_info // pointer to PROCESS_INFORMATION that will contain the new process identification
  634. );
  635. bool debugger_run_ok = false;
  636. if( created )
  637. {
  638. DWORD ret_code = ::WaitForSingleObject( dbg_init_done_ev, INFINITE );
  639. debugger_run_ok = ( ret_code == WAIT_OBJECT_0 );
  640. }
  641. safe_handle_obj.close_handle();
  642. if( !created || !debugger_run_ok )
  643. return false;
  644. if( break_or_continue )
  645. debugger_break();
  646. return true;
  647. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  648. char init_done_lock_fn[] = "/tmp/btl_dbg_init_done_XXXXXX";
  649. fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
  650. if( init_done_lock_fd == -1 )
  651. return false;
  652. pid_t child_pid = fork();
  653. if( child_pid == -1 )
  654. return false;
  655. if( child_pid != 0 ) { // parent process - here we will start the debugger
  656. dbg_startup_info dsi;
  657. process_info pi( child_pid );
  658. if( pi.binary_path().is_empty() )
  659. ::exit( -1 );
  660. dsi.pid = child_pid;
  661. dsi.break_or_continue = break_or_continue;
  662. dsi.binary_path = pi.binary_path();
  663. dsi.display = ::getenv( "DISPLAY" );
  664. dsi.init_done_lock = init_done_lock_fn;
  665. dbg_starter starter = s_info.m_dbg_starter_reg[s_info.p_dbg];
  666. if( !!starter )
  667. starter( dsi );
  668. ::perror( "Boost.Test execution monitor failed to start a debugger:" );
  669. ::exit( -1 );
  670. }
  671. // child process - here we will continue our test module execution ; // !! ?? should it be vice versa
  672. while( ::access( init_done_lock_fn, F_OK ) == 0 ) {
  673. struct timeval to = { 0, 100 };
  674. ::select( 0, 0, 0, 0, &to );
  675. }
  676. // char dummy;
  677. // while( ::read( init_done_lock_fd, &dummy, sizeof(char) ) == 0 );
  678. if( break_or_continue )
  679. debugger_break();
  680. return true;
  681. #else // ****************************************************** default
  682. (void) break_or_continue; // silence 'unused variable' warning
  683. return false;
  684. #endif
  685. }
  686. //____________________________________________________________________________//
  687. // ************************************************************************** //
  688. // ************** switch on/off detect memory leaks feature ************** //
  689. // ************************************************************************** //
  690. void
  691. detect_memory_leaks( bool on_off, unit_test::const_string report_file )
  692. {
  693. boost::ignore_unused( on_off );
  694. #ifdef BOOST_MS_CRT_BASED_DEBUG
  695. int flags = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
  696. if( !on_off )
  697. flags &= ~_CRTDBG_LEAK_CHECK_DF;
  698. else {
  699. flags |= _CRTDBG_LEAK_CHECK_DF;
  700. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  701. if( report_file.is_empty() )
  702. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  703. else {
  704. HANDLE hreport_f = ::CreateFileA( report_file.begin(),
  705. GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  706. _CrtSetReportFile(_CRT_WARN, hreport_f );
  707. }
  708. }
  709. _CrtSetDbgFlag ( flags );
  710. #else
  711. boost::ignore_unused( report_file );
  712. #endif // BOOST_MS_CRT_BASED_DEBUG
  713. }
  714. //____________________________________________________________________________//
  715. // ************************************************************************** //
  716. // ************** cause program to break execution in ************** //
  717. // ************** debugger at specific allocation point ************** //
  718. // ************************************************************************** //
  719. void
  720. break_memory_alloc( long mem_alloc_order_num )
  721. {
  722. boost::ignore_unused( mem_alloc_order_num );
  723. #ifdef BOOST_MS_CRT_BASED_DEBUG
  724. // only set the value if one was supplied (do not use default used by UTF just as a indicator to enable leak detection)
  725. if( mem_alloc_order_num > 1 )
  726. _CrtSetBreakAlloc( mem_alloc_order_num );
  727. #endif // BOOST_MS_CRT_BASED_DEBUG
  728. }
  729. //____________________________________________________________________________//
  730. } // namespace debug
  731. } // namespace boost
  732. #include <boost/test/detail/enable_warnings.hpp>
  733. #endif // BOOST_TEST_DEBUG_API_IPP_112006GER