debug.ipp 32 KB

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