debug.ipp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. const mode_t cur_umask = ::umask( S_IRWXO | S_IRWXG );
  303. fd_holder cmd_fd( ::mkstemp( cmd_file_name ) );
  304. ::umask( cur_umask );
  305. if( cmd_fd == -1 )
  306. return 0;
  307. #define WRITE_STR( str ) if( ::write( cmd_fd, str.begin(), str.size() ) == -1 ) return 0;
  308. #define WRITE_CSTR( str ) if( ::write( cmd_fd, str, sizeof( str )-1 ) == -1 ) return 0;
  309. WRITE_CSTR( "file " );
  310. WRITE_STR( dsi.binary_path );
  311. WRITE_CSTR( "\nattach " );
  312. WRITE_STR( pid_str );
  313. WRITE_CSTR( "\nshell unlink " );
  314. WRITE_STR( dsi.init_done_lock );
  315. WRITE_CSTR( "\ncont" );
  316. if( dsi.break_or_continue )
  317. WRITE_CSTR( "\nup 4" );
  318. WRITE_CSTR( "\necho \\n" ); // !! ??
  319. WRITE_CSTR( "\nlist -" );
  320. WRITE_CSTR( "\nlist" );
  321. WRITE_CSTR( "\nshell unlink " );
  322. WRITE_CSTR( cmd_file_name );
  323. return cmd_file_name;
  324. }
  325. //____________________________________________________________________________//
  326. static void
  327. start_gdb_in_console( dbg_startup_info const& dsi )
  328. {
  329. char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
  330. if( !cmnd_file_name )
  331. return;
  332. safe_execlp( "gdb", "-q", "-x", cmnd_file_name, 0 );
  333. }
  334. //____________________________________________________________________________//
  335. static void
  336. start_gdb_in_xterm( dbg_startup_info const& dsi )
  337. {
  338. char const* title = prepare_window_title( dsi );
  339. char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
  340. if( !title || !cmnd_file_name )
  341. return;
  342. safe_execlp( "xterm", "-T", title, "-display", dsi.display.begin(),
  343. "-bg", "black", "-fg", "white", "-geometry", "88x30+10+10", "-fn", "9x15", "-e",
  344. "gdb", "-q", "-x", cmnd_file_name, 0 );
  345. }
  346. //____________________________________________________________________________//
  347. static void
  348. start_gdb_in_emacs( dbg_startup_info const& dsi )
  349. {
  350. char const* cmnd_file_name = prepare_gdb_cmnd_file( dsi );
  351. if( !cmnd_file_name )
  352. return;
  353. char dbg_cmd_buff[500]; // !! ??
  354. ::snprintf( dbg_cmd_buff, sizeof(dbg_cmd_buff), "(progn (gdb \"gdb -q -x %s\"))", cmnd_file_name );
  355. start_debugger_in_emacs( dsi, "emacs", dbg_cmd_buff );
  356. }
  357. //____________________________________________________________________________//
  358. static void
  359. start_gdb_in_xemacs( dbg_startup_info const& )
  360. {
  361. // !! ??
  362. }
  363. //____________________________________________________________________________//
  364. // ************************************************************************** //
  365. // ************** dbx starters ************** //
  366. // ************************************************************************** //
  367. static char const*
  368. prepare_dbx_cmd_line( dbg_startup_info const& dsi, bool list_source = true )
  369. {
  370. static char cmd_line_buff[500]; // !! ??
  371. ::snprintf( cmd_line_buff, sizeof(cmd_line_buff), "unlink %s;cont;%s%s",
  372. dsi.init_done_lock.begin(),
  373. dsi.break_or_continue ? "up 2;": "",
  374. list_source ? "echo \" \";list -w3;" : "" );
  375. return cmd_line_buff;
  376. }
  377. //____________________________________________________________________________//
  378. static void
  379. start_dbx_in_console( dbg_startup_info const& dsi )
  380. {
  381. char pid_buff[16];
  382. ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
  383. safe_execlp( "dbx", "-q", "-c", prepare_dbx_cmd_line( dsi ), dsi.binary_path.begin(), pid_buff, 0 );
  384. }
  385. //____________________________________________________________________________//
  386. static void
  387. start_dbx_in_xterm( dbg_startup_info const& dsi )
  388. {
  389. char const* title = prepare_window_title( dsi );
  390. if( !title )
  391. return;
  392. char pid_buff[16]; // !! ??
  393. ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
  394. safe_execlp( "xterm", "-T", title, "-display", dsi.display.begin(),
  395. "-bg", "black", "-fg", "white", "-geometry", "88x30+10+10", "-fn", "9x15", "-e",
  396. "dbx", "-q", "-c", prepare_dbx_cmd_line( dsi ), dsi.binary_path.begin(), pid_buff, 0 );
  397. }
  398. //____________________________________________________________________________//
  399. static void
  400. start_dbx_in_emacs( dbg_startup_info const& /*dsi*/ )
  401. {
  402. // char dbg_cmd_buff[500]; // !! ??
  403. //
  404. // ::snprintf( dbg_cmd_buff, sizeof(dbg_cmd_buff), "(progn (dbx \"dbx -q -c cont %s %ld\"))", dsi.binary_path.begin(), dsi.pid );
  405. // start_debugger_in_emacs( dsi, "emacs", dbg_cmd_buff );
  406. }
  407. //____________________________________________________________________________//
  408. static void
  409. start_dbx_in_xemacs( dbg_startup_info const& )
  410. {
  411. // !! ??
  412. }
  413. //____________________________________________________________________________//
  414. static void
  415. start_dbx_in_ddd( dbg_startup_info const& dsi )
  416. {
  417. char const* title = prepare_window_title( dsi );
  418. if( !title )
  419. return;
  420. char pid_buff[16]; // !! ??
  421. ::snprintf( pid_buff, sizeof(pid_buff), "%ld", dsi.pid );
  422. safe_execlp( "ddd", "-display", dsi.display.begin(),
  423. "--dbx", "-q", "-c", prepare_dbx_cmd_line( dsi, false ), dsi.binary_path.begin(), pid_buff, 0 );
  424. }
  425. //____________________________________________________________________________//
  426. // ************************************************************************** //
  427. // ************** debug::info_t ************** //
  428. // ************************************************************************** //
  429. static struct info_t {
  430. // Constructor
  431. info_t();
  432. // Public properties
  433. unit_test::readwrite_property<std::string> p_dbg;
  434. // Data members
  435. std::map<std::string,dbg_starter> m_dbg_starter_reg;
  436. } s_info;
  437. //____________________________________________________________________________//
  438. info_t::info_t()
  439. {
  440. p_dbg.value = ::getenv( "DISPLAY" )
  441. ? std::string( BOOST_STRINGIZE( BOOST_TEST_GUI_DBG ) )
  442. : std::string( BOOST_STRINGIZE( BOOST_TEST_CNL_DBG ) );
  443. m_dbg_starter_reg[std::string("gdb")] = &start_gdb_in_console;
  444. m_dbg_starter_reg[std::string("gdb-emacs")] = &start_gdb_in_emacs;
  445. m_dbg_starter_reg[std::string("gdb-xterm")] = &start_gdb_in_xterm;
  446. m_dbg_starter_reg[std::string("gdb-xemacs")] = &start_gdb_in_xemacs;
  447. m_dbg_starter_reg[std::string("dbx")] = &start_dbx_in_console;
  448. m_dbg_starter_reg[std::string("dbx-emacs")] = &start_dbx_in_emacs;
  449. m_dbg_starter_reg[std::string("dbx-xterm")] = &start_dbx_in_xterm;
  450. m_dbg_starter_reg[std::string("dbx-xemacs")] = &start_dbx_in_xemacs;
  451. m_dbg_starter_reg[std::string("dbx-ddd")] = &start_dbx_in_ddd;
  452. }
  453. //____________________________________________________________________________//
  454. #endif
  455. } // local namespace
  456. // ************************************************************************** //
  457. // ************** check if program is running under debugger ************** //
  458. // ************************************************************************** //
  459. bool
  460. under_debugger()
  461. {
  462. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  463. return !!s_info.m_is_debugger_present && s_info.m_is_debugger_present();
  464. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  465. // !! ?? could/should we cache the result somehow?
  466. const_string dbg_list = BOOST_TEST_STRINGIZE( BOOST_TEST_DBG_LIST );
  467. pid_t pid = ::getpid();
  468. while( pid != 0 ) {
  469. process_info pi( pid );
  470. // !! ?? should we use tokenizer here instead?
  471. if( dbg_list.find( pi.binary_name() ) != const_string::npos )
  472. return true;
  473. pid = (pi.parent_pid() == pid ? 0 : pi.parent_pid());
  474. }
  475. return false;
  476. #else // ****************************************************** default
  477. return false;
  478. #endif
  479. }
  480. //____________________________________________________________________________//
  481. // ************************************************************************** //
  482. // ************** cause program to break execution ************** //
  483. // ************** in debugger at call point ************** //
  484. // ************************************************************************** //
  485. void
  486. debugger_break()
  487. {
  488. // !! ?? auto-start debugger?
  489. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  490. #if defined(__GNUC__) && !defined(__MINGW32__) || \
  491. defined(__INTEL_COMPILER)
  492. # define BOOST_DEBUG_BREAK __debugbreak
  493. #else
  494. # define BOOST_DEBUG_BREAK DebugBreak
  495. #endif
  496. #ifndef __MINGW32__
  497. if( !under_debugger() ) {
  498. __try {
  499. __try {
  500. BOOST_DEBUG_BREAK();
  501. }
  502. __except( UnhandledExceptionFilter(GetExceptionInformation()) )
  503. {
  504. // User opted to ignore the breakpoint
  505. return;
  506. }
  507. }
  508. __except (EXCEPTION_EXECUTE_HANDLER)
  509. {
  510. // If we got here, the user has pushed Debug. Debugger is already attached to our process and we
  511. // continue to let the another BOOST_DEBUG_BREAK to be called.
  512. }
  513. }
  514. #endif
  515. BOOST_DEBUG_BREAK();
  516. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  517. ::kill( ::getpid(), SIGTRAP );
  518. #else // ****************************************************** default
  519. #endif
  520. }
  521. //____________________________________________________________________________//
  522. // ************************************************************************** //
  523. // ************** console debugger setup ************** //
  524. // ************************************************************************** //
  525. #if defined(BOOST_UNIX_BASED_DEBUG) // ************************ UNIX
  526. std::string
  527. set_debugger( unit_test::const_string dbg_id, dbg_starter s )
  528. {
  529. std::string old = s_info.p_dbg;
  530. assign_op( s_info.p_dbg.value, dbg_id, 0 );
  531. if( !!s )
  532. s_info.m_dbg_starter_reg[s_info.p_dbg.get()] = s;
  533. return old;
  534. }
  535. #else // ***************************************************** default
  536. std::string
  537. set_debugger( unit_test::const_string, dbg_starter )
  538. {
  539. return std::string();
  540. }
  541. #endif
  542. //____________________________________________________________________________//
  543. // ************************************************************************** //
  544. // ************** attach debugger to the current process ************** //
  545. // ************************************************************************** //
  546. #if defined(BOOST_WIN32_BASED_DEBUG)
  547. struct safe_handle_helper
  548. {
  549. HANDLE& handle;
  550. safe_handle_helper(HANDLE &handle_) : handle(handle_) {}
  551. void close_handle()
  552. {
  553. if( handle != INVALID_HANDLE_VALUE )
  554. {
  555. ::CloseHandle( handle );
  556. handle = INVALID_HANDLE_VALUE;
  557. }
  558. }
  559. ~safe_handle_helper()
  560. {
  561. close_handle();
  562. }
  563. };
  564. #endif
  565. bool
  566. attach_debugger( bool break_or_continue )
  567. {
  568. if( under_debugger() )
  569. return false;
  570. #if defined(BOOST_WIN32_BASED_DEBUG) // *********************** WIN32
  571. const int MAX_CMD_LINE = 200;
  572. // *************************************************** //
  573. // Debugger "ready" event
  574. SECURITY_ATTRIBUTES attr;
  575. attr.nLength = sizeof(attr);
  576. attr.lpSecurityDescriptor = NULL;
  577. attr.bInheritHandle = true;
  578. // manual resettable, initially non signaled, unnamed event,
  579. // that will signal me that debugger initialization is done
  580. HANDLE dbg_init_done_ev = ::CreateEvent(
  581. &attr, // pointer to security attributes
  582. true, // flag for manual-reset event
  583. false, // flag for initial state
  584. NULL // pointer to event-object name
  585. );
  586. if( !dbg_init_done_ev )
  587. return false;
  588. safe_handle_helper safe_handle_obj( dbg_init_done_ev );
  589. // *************************************************** //
  590. // Debugger command line format
  591. HKEY reg_key;
  592. if( !s_info.m_reg_open_key || (*s_info.m_reg_open_key)(
  593. HKEY_LOCAL_MACHINE, // handle of open key
  594. "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", // name of subkey to open
  595. &reg_key ) != ERROR_SUCCESS ) // address of handle of open key
  596. return false;
  597. char format[MAX_CMD_LINE];
  598. DWORD format_size = MAX_CMD_LINE;
  599. DWORD type = REG_SZ;
  600. bool b_read_key = s_info.m_reg_query_value &&
  601. ((*s_info.m_reg_query_value)(
  602. reg_key, // handle of open key
  603. "Debugger", // name of subkey to query
  604. 0, // reserved
  605. &type, // value type
  606. (LPBYTE)format, // buffer for returned string
  607. &format_size ) == ERROR_SUCCESS ); // in: buffer size; out: actual size of returned string
  608. if( !s_info.m_reg_close_key || (*s_info.m_reg_close_key)( reg_key ) != ERROR_SUCCESS )
  609. return false;
  610. if( !b_read_key )
  611. return false;
  612. // *************************************************** //
  613. // Debugger command line
  614. char cmd_line[MAX_CMD_LINE];
  615. std::sprintf( cmd_line, format, ::GetCurrentProcessId(), dbg_init_done_ev );
  616. // *************************************************** //
  617. // Debugger window parameters
  618. STARTUPINFOA startup_info;
  619. std::memset( &startup_info, 0, sizeof(startup_info) );
  620. startup_info.cb = sizeof(startup_info);
  621. startup_info.dwFlags = STARTF_USESHOWWINDOW;
  622. startup_info.wShowWindow = SW_SHOWNORMAL;
  623. // debugger process s_info
  624. PROCESS_INFORMATION debugger_info;
  625. bool created = !!::CreateProcessA(
  626. NULL, // pointer to name of executable module; NULL - use the one in command line
  627. cmd_line, // pointer to command line string
  628. NULL, // pointer to process security attributes; NULL - debugger's handle can't be inherited
  629. NULL, // pointer to thread security attributes; NULL - debugger's handle can't be inherited
  630. true, // debugger inherit opened handles
  631. 0, // priority flags; 0 - normal priority
  632. NULL, // pointer to new environment block; NULL - use this process environment
  633. NULL, // pointer to current directory name; NULL - use this process correct directory
  634. &startup_info, // pointer to STARTUPINFO that specifies main window appearance
  635. &debugger_info // pointer to PROCESS_INFORMATION that will contain the new process identification
  636. );
  637. bool debugger_run_ok = false;
  638. if( created )
  639. {
  640. DWORD ret_code = ::WaitForSingleObject( dbg_init_done_ev, INFINITE );
  641. debugger_run_ok = ( ret_code == WAIT_OBJECT_0 );
  642. }
  643. safe_handle_obj.close_handle();
  644. if( !created || !debugger_run_ok )
  645. return false;
  646. if( break_or_continue )
  647. debugger_break();
  648. return true;
  649. #elif defined(BOOST_UNIX_BASED_DEBUG) // ********************** UNIX
  650. char init_done_lock_fn[] = "/tmp/btl_dbg_init_done_XXXXXX";
  651. const mode_t cur_umask = ::umask( S_IRWXO | S_IRWXG );
  652. fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
  653. ::umask( cur_umask );
  654. if( init_done_lock_fd == -1 )
  655. return false;
  656. pid_t child_pid = fork();
  657. if( child_pid == -1 )
  658. return false;
  659. if( child_pid != 0 ) { // parent process - here we will start the debugger
  660. dbg_startup_info dsi;
  661. process_info pi( child_pid );
  662. if( pi.binary_path().is_empty() )
  663. ::exit( -1 );
  664. dsi.pid = child_pid;
  665. dsi.break_or_continue = break_or_continue;
  666. dsi.binary_path = pi.binary_path();
  667. dsi.display = ::getenv( "DISPLAY" );
  668. dsi.init_done_lock = init_done_lock_fn;
  669. dbg_starter starter = s_info.m_dbg_starter_reg[s_info.p_dbg];
  670. if( !!starter )
  671. starter( dsi );
  672. ::perror( "Boost.Test execution monitor failed to start a debugger:" );
  673. ::exit( -1 );
  674. }
  675. // child process - here we will continue our test module execution ; // !! ?? should it be vice versa
  676. while( ::access( init_done_lock_fn, F_OK ) == 0 ) {
  677. struct timeval to = { 0, 100 };
  678. ::select( 0, 0, 0, 0, &to );
  679. }
  680. // char dummy;
  681. // while( ::read( init_done_lock_fd, &dummy, sizeof(char) ) == 0 );
  682. if( break_or_continue )
  683. debugger_break();
  684. return true;
  685. #else // ****************************************************** default
  686. (void) break_or_continue; // silence 'unused variable' warning
  687. return false;
  688. #endif
  689. }
  690. //____________________________________________________________________________//
  691. // ************************************************************************** //
  692. // ************** switch on/off detect memory leaks feature ************** //
  693. // ************************************************************************** //
  694. void
  695. detect_memory_leaks( bool on_off, unit_test::const_string report_file )
  696. {
  697. boost::ignore_unused( on_off );
  698. #ifdef BOOST_MS_CRT_BASED_DEBUG
  699. int flags = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
  700. if( !on_off )
  701. flags &= ~_CRTDBG_LEAK_CHECK_DF;
  702. else {
  703. flags |= _CRTDBG_LEAK_CHECK_DF;
  704. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  705. if( report_file.is_empty() )
  706. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  707. else {
  708. HANDLE hreport_f = ::CreateFileA( report_file.begin(),
  709. GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  710. _CrtSetReportFile(_CRT_WARN, hreport_f );
  711. }
  712. }
  713. _CrtSetDbgFlag ( flags );
  714. #else
  715. boost::ignore_unused( report_file );
  716. #endif // BOOST_MS_CRT_BASED_DEBUG
  717. }
  718. //____________________________________________________________________________//
  719. // ************************************************************************** //
  720. // ************** cause program to break execution in ************** //
  721. // ************** debugger at specific allocation point ************** //
  722. // ************************************************************************** //
  723. void
  724. break_memory_alloc( long mem_alloc_order_num )
  725. {
  726. boost::ignore_unused( mem_alloc_order_num );
  727. #ifdef BOOST_MS_CRT_BASED_DEBUG
  728. // only set the value if one was supplied (do not use default used by UTF just as a indicator to enable leak detection)
  729. if( mem_alloc_order_num > 1 )
  730. _CrtSetBreakAlloc( mem_alloc_order_num );
  731. #endif // BOOST_MS_CRT_BASED_DEBUG
  732. }
  733. //____________________________________________________________________________//
  734. } // namespace debug
  735. } // namespace boost
  736. #include <boost/test/detail/enable_warnings.hpp>
  737. #endif // BOOST_TEST_DEBUG_API_IPP_112006GER