debug.ipp 31 KB

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