ch_malloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* slapi_ch_malloc.c - malloc routines that test returns from malloc and friends */
  42. #include <errno.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h> /* strdup */
  46. #include <sys/types.h>
  47. #ifndef _WIN32
  48. #include <sys/socket.h>
  49. #endif
  50. #undef DEBUG /* disable counters */
  51. #include <prcountr.h>
  52. #include "slap.h"
  53. static int counters_created= 0;
  54. PR_DEFINE_COUNTER(slapi_ch_counter_malloc);
  55. PR_DEFINE_COUNTER(slapi_ch_counter_calloc);
  56. PR_DEFINE_COUNTER(slapi_ch_counter_realloc);
  57. PR_DEFINE_COUNTER(slapi_ch_counter_strdup);
  58. PR_DEFINE_COUNTER(slapi_ch_counter_free);
  59. PR_DEFINE_COUNTER(slapi_ch_counter_created);
  60. PR_DEFINE_COUNTER(slapi_ch_counter_exist);
  61. #define OOM_PREALLOC_SIZE 65536
  62. static void *oom_emergency_area = NULL;
  63. static PRLock *oom_emergency_lock = NULL;
  64. #if defined(_WIN32)
  65. static int recording= 0;
  66. #endif
  67. #define SLAPD_MODULE "memory allocator"
  68. static const char* const oom_advice =
  69. "\nThe server has probably allocated all available virtual memory. To solve\n"
  70. "this problem, make more virtual memory available to your server, or reduce\n"
  71. "one or more of the following server configuration settings:\n"
  72. " nsslapd-cachesize (Database Settings - Maximum entries in cache)\n"
  73. " nsslapd-cachememsize (Database Settings - Memory available for cache)\n"
  74. " nsslapd-dbcachesize (LDBM Plug-in Settings - Maximum cache size)\n"
  75. " nsslapd-import-cachesize (LDBM Plug-in Settings - Import cache size).\n"
  76. "Can't recover; calling exit(1).\n";
  77. #if defined(_WIN32) && defined(DEBUG)
  78. static void add_memory_record(void *p,unsigned long size);
  79. static void remove_memory_record(void *p);
  80. static int memory_record_dump( caddr_t data, caddr_t arg );
  81. static int memory_record_delete( caddr_t data, caddr_t arg );
  82. #endif
  83. static void
  84. create_counters()
  85. {
  86. PR_CREATE_COUNTER(slapi_ch_counter_malloc,"slapi_ch","malloc","");
  87. PR_CREATE_COUNTER(slapi_ch_counter_calloc,"slapi_ch","calloc","");
  88. PR_CREATE_COUNTER(slapi_ch_counter_realloc,"slapi_ch","realloc","");
  89. PR_CREATE_COUNTER(slapi_ch_counter_strdup,"slapi_ch","strdup","");
  90. PR_CREATE_COUNTER(slapi_ch_counter_free,"slapi_ch","free","");
  91. PR_CREATE_COUNTER(slapi_ch_counter_created,"slapi_ch","created","");
  92. PR_CREATE_COUNTER(slapi_ch_counter_exist,"slapi_ch","exist","");
  93. /* ensure that we have space to allow for shutdown calls to malloc()
  94. * from should we run out of memory.
  95. */
  96. if (oom_emergency_area == NULL) {
  97. oom_emergency_area = malloc(OOM_PREALLOC_SIZE);
  98. }
  99. oom_emergency_lock = PR_NewLock();
  100. }
  101. /* called when we have just detected an out of memory condition, before
  102. * we make any other library calls. Note that LDAPDebug() calls malloc,
  103. * indirectly. By making 64KB free, we should be able to have a few
  104. * mallocs' succeed before we shut down.
  105. */
  106. void oom_occurred(void)
  107. {
  108. int tmp_errno = errno; /* callers will need the error from malloc */
  109. if (oom_emergency_lock == NULL) return;
  110. PR_Lock(oom_emergency_lock);
  111. if (oom_emergency_area) {
  112. free(oom_emergency_area);
  113. oom_emergency_area = NULL;
  114. }
  115. PR_Unlock(oom_emergency_lock);
  116. errno = tmp_errno;
  117. }
  118. static void
  119. log_negative_alloc_msg( const char *op, const char *units, unsigned long size )
  120. {
  121. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  122. "cannot %s %lu %s;\n"
  123. "trying to allocate 0 or a negative number of %s is not portable and\n"
  124. "gives different results on different platforms.\n",
  125. op, size, units, units );
  126. }
  127. #if !defined(MEMPOOL_EXPERIMENTAL)
  128. char *
  129. slapi_ch_malloc(
  130. unsigned long size
  131. )
  132. {
  133. char *newmem;
  134. if (size <= 0) {
  135. log_negative_alloc_msg( "malloc", "bytes", size );
  136. return 0;
  137. }
  138. if ( (newmem = (char *) malloc( size )) == NULL ) {
  139. int oserr = errno;
  140. oom_occurred();
  141. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  142. "malloc of %lu bytes failed; OS error %d (%s)%s\n",
  143. size, oserr, slapd_system_strerror( oserr ), oom_advice );
  144. exit( 1 );
  145. }
  146. if(!counters_created)
  147. {
  148. create_counters();
  149. counters_created= 1;
  150. }
  151. PR_INCREMENT_COUNTER(slapi_ch_counter_malloc);
  152. PR_INCREMENT_COUNTER(slapi_ch_counter_created);
  153. PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
  154. #if defined(_WIN32) && defined(DEBUG)
  155. if(recording)
  156. {
  157. add_memory_record(newmem,size);
  158. }
  159. #endif
  160. return( newmem );
  161. }
  162. char *
  163. slapi_ch_realloc(
  164. char *block,
  165. unsigned long size
  166. )
  167. {
  168. char *newmem;
  169. if ( block == NULL ) {
  170. return( slapi_ch_malloc( size ) );
  171. }
  172. if (size <= 0) {
  173. log_negative_alloc_msg( "realloc", "bytes", size );
  174. return block;
  175. }
  176. if ( (newmem = (char *) realloc( block, size )) == NULL ) {
  177. int oserr = errno;
  178. oom_occurred();
  179. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  180. "realloc of %lu bytes failed; OS error %d (%s)%s\n",
  181. size, oserr, slapd_system_strerror( oserr ), oom_advice );
  182. exit( 1 );
  183. }
  184. if(!counters_created)
  185. {
  186. create_counters();
  187. counters_created= 1;
  188. }
  189. PR_INCREMENT_COUNTER(slapi_ch_counter_realloc);
  190. #if defined(_WIN32) && defined(DEBUG)
  191. if(recording)
  192. {
  193. remove_memory_record(block);
  194. add_memory_record(newmem,size);
  195. }
  196. #endif
  197. return( newmem );
  198. }
  199. char *
  200. slapi_ch_calloc(
  201. unsigned long nelem,
  202. unsigned long size
  203. )
  204. {
  205. char *newmem;
  206. if (size <= 0) {
  207. log_negative_alloc_msg( "calloc", "bytes", size );
  208. return 0;
  209. }
  210. if (nelem <= 0) {
  211. log_negative_alloc_msg( "calloc", "elements", nelem );
  212. return 0;
  213. }
  214. if ( (newmem = (char *) calloc( nelem, size )) == NULL ) {
  215. int oserr = errno;
  216. oom_occurred();
  217. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  218. "calloc of %lu elems of %lu bytes failed; OS error %d (%s)%s\n",
  219. nelem, size, oserr, slapd_system_strerror( oserr ), oom_advice );
  220. exit( 1 );
  221. }
  222. if(!counters_created)
  223. {
  224. create_counters();
  225. counters_created= 1;
  226. }
  227. PR_INCREMENT_COUNTER(slapi_ch_counter_calloc);
  228. PR_INCREMENT_COUNTER(slapi_ch_counter_created);
  229. PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
  230. #if defined(_WIN32) && defined(DEBUG)
  231. if(recording)
  232. {
  233. add_memory_record(newmem,size);
  234. }
  235. #endif
  236. return( newmem );
  237. }
  238. char*
  239. slapi_ch_strdup ( const char* s1)
  240. {
  241. char* newmem;
  242. /* strdup pukes on NULL strings...bail out now */
  243. if(NULL == s1)
  244. return NULL;
  245. newmem = strdup (s1);
  246. if (newmem == NULL) {
  247. int oserr = errno;
  248. oom_occurred();
  249. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  250. "strdup of %lu characters failed; OS error %d (%s)%s\n",
  251. (unsigned long)strlen(s1), oserr, slapd_system_strerror( oserr ),
  252. oom_advice );
  253. exit (1);
  254. }
  255. if(!counters_created)
  256. {
  257. create_counters();
  258. counters_created= 1;
  259. }
  260. PR_INCREMENT_COUNTER(slapi_ch_counter_strdup);
  261. PR_INCREMENT_COUNTER(slapi_ch_counter_created);
  262. PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
  263. #if defined(_WIN32) && defined(DEBUG)
  264. if(recording)
  265. {
  266. add_memory_record(newmem,strlen(s1)+1);
  267. }
  268. #endif
  269. return newmem;
  270. }
  271. char*
  272. slapi_ch_strndup ( const char* s1, size_t n)
  273. {
  274. char* newmem;
  275. /* strdup pukes on NULL strings...bail out now */
  276. if ((NULL == s1) || (0 == n)) {
  277. return NULL;
  278. }
  279. newmem = strndup (s1, n);
  280. if (newmem == NULL) {
  281. int oserr = errno;
  282. oom_occurred();
  283. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  284. "strdup of %lu characters failed; OS error %d (%s)%s\n",
  285. (unsigned long)n, oserr, slapd_system_strerror( oserr ),
  286. oom_advice );
  287. exit (1);
  288. }
  289. if(!counters_created)
  290. {
  291. create_counters();
  292. counters_created= 1;
  293. }
  294. PR_INCREMENT_COUNTER(slapi_ch_counter_strdup);
  295. PR_INCREMENT_COUNTER(slapi_ch_counter_created);
  296. PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
  297. #if defined(_WIN32) && defined(DEBUG)
  298. if(recording)
  299. {
  300. add_memory_record(newmem,strlen(s1)+1);
  301. }
  302. #endif
  303. return newmem;
  304. }
  305. #endif /* !MEMPOOL_EXPERIMENTAL */
  306. struct berval*
  307. slapi_ch_bvdup (const struct berval* v)
  308. {
  309. struct berval* newberval = ber_bvdup ((struct berval *)v);
  310. if (newberval == NULL) {
  311. int oserr = errno;
  312. oom_occurred();
  313. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  314. "ber_bvdup of %lu bytes failed; OS error %d (%s)%s\n",
  315. (unsigned long)v->bv_len, oserr, slapd_system_strerror( oserr ),
  316. oom_advice );
  317. exit( 1 );
  318. }
  319. return newberval;
  320. }
  321. struct berval**
  322. slapi_ch_bvecdup (struct berval** v)
  323. {
  324. struct berval** newberval = NULL;
  325. if (v != NULL) {
  326. size_t i = 0;
  327. while (v[i] != NULL) ++i;
  328. newberval = (struct berval**) slapi_ch_malloc ((i + 1) * sizeof (struct berval*));
  329. newberval[i] = NULL;
  330. while (i-- > 0) {
  331. newberval[i] = slapi_ch_bvdup (v[i]);
  332. }
  333. }
  334. return newberval;
  335. }
  336. #if !defined(MEMPOOL_EXPERIMENTAL)
  337. /*
  338. * Function: slapi_ch_free
  339. *
  340. * Returns: nothing
  341. *
  342. * Description: frees the pointer, and then sets it to NULL to
  343. * prevent free-memory writes.
  344. * Note: pass in the address of the pointer you want to free.
  345. * Note: you can pass in null pointers, it's cool.
  346. */
  347. void
  348. slapi_ch_free(void **ptr)
  349. {
  350. if (ptr==NULL || *ptr == NULL){
  351. return;
  352. }
  353. #if defined(_WIN32) && defined(DEBUG)
  354. if(recording)
  355. {
  356. remove_memory_record(*ptr);
  357. }
  358. #endif
  359. free (*ptr);
  360. *ptr = NULL;
  361. if(!counters_created)
  362. {
  363. create_counters();
  364. counters_created= 1;
  365. }
  366. PR_INCREMENT_COUNTER(slapi_ch_counter_free);
  367. PR_DECREMENT_COUNTER(slapi_ch_counter_exist);
  368. return;
  369. }
  370. #endif /* !MEMPOOL_EXPERIMENTAL */
  371. /* just like slapi_ch_free, takes the address of the struct berval pointer */
  372. void
  373. slapi_ch_bvfree(struct berval** v)
  374. {
  375. if (v == NULL || *v == NULL)
  376. return;
  377. slapi_ch_free((void **)&((*v)->bv_val));
  378. slapi_ch_free((void **)v);
  379. return;
  380. }
  381. /* just like slapi_ch_free, but the argument is the address of a string
  382. This helps with compile time error checking
  383. */
  384. void
  385. slapi_ch_free_string(char **s)
  386. {
  387. slapi_ch_free((void **)s);
  388. }
  389. /*
  390. This function is just like PR_smprintf. It works like sprintf
  391. except that it allocates enough memory to hold the result
  392. string and returns that allocated memory to the caller. The
  393. caller must use slapi_ch_free_string to free the memory.
  394. It should only be used in those situations that will eventually free
  395. the memory using slapi_ch_free_string e.g. allocating a string
  396. that will be freed as part of pblock cleanup, or passed in to create
  397. a Slapi_DN, or things of that nature. If you have control of the
  398. flow such that the memory will be allocated and freed in the same
  399. scope, better to just use PR_smprintf and PR_smprintf_free instead
  400. because it is likely faster.
  401. */
  402. /*
  403. This implementation is the same as PR_smprintf.
  404. The above comment does not apply to this function for now.
  405. see [150809] for more details.
  406. WARNING - with this fix, this means we are now mixing PR_Malloc with
  407. slapi_ch_free. Which is ok for now - they both use malloc/free from
  408. the operating system. But if this changes in the future, this
  409. function will have to change as well.
  410. */
  411. #if !defined(MEMPOOL_EXPERIMENTAL)
  412. char *
  413. slapi_ch_smprintf(const char *fmt, ...)
  414. {
  415. char *p = NULL;
  416. va_list ap;
  417. if (NULL == fmt) {
  418. return NULL;
  419. }
  420. va_start(ap, fmt);
  421. p = PR_vsmprintf(fmt, ap);
  422. va_end(ap);
  423. return p;
  424. }
  425. #endif
  426. /* ========================= NT Specific Leak Checking Code ================================== */
  427. #if defined(_WIN32) && defined(DEBUG)
  428. #define STOP_TRAVERSAL -2
  429. #define MR_CALL_STACK 16
  430. #define MR_DUMP_AMOUNT 16
  431. static Avlnode *mr_tree= NULL;
  432. static PRLock *mr_tree_lock= NULL;
  433. #endif
  434. void
  435. slapi_ch_start_recording()
  436. {
  437. #if defined(_WIN32) && defined(DEBUG)
  438. if(mr_tree_lock==NULL)
  439. {
  440. mr_tree_lock = PR_NewLock();
  441. }
  442. PR_Lock( mr_tree_lock );
  443. recording= 1;
  444. PR_Unlock( mr_tree_lock );
  445. #endif
  446. }
  447. void
  448. slapi_ch_stop_recording()
  449. {
  450. #if defined(_WIN32) && defined(DEBUG)
  451. PR_Lock( mr_tree_lock );
  452. recording= 0;
  453. avl_apply( mr_tree, memory_record_dump, NULL, STOP_TRAVERSAL, AVL_INORDER );
  454. avl_free( mr_tree, memory_record_delete );
  455. mr_tree= NULL;
  456. PR_Unlock( mr_tree_lock );
  457. #endif
  458. }
  459. #if defined(_WIN32) && defined(DEBUG)
  460. struct memory_record
  461. {
  462. void *p;
  463. unsigned long size;
  464. DWORD ra[MR_CALL_STACK];
  465. };
  466. static void
  467. mr_to_hex_dump(char* dest, void *addr, int size, int MaxBytes)
  468. {
  469. int i;
  470. for (i=0; i<MaxBytes; i++)
  471. {
  472. if(i<size)
  473. {
  474. wsprintf(dest+i*2, "%02x", ((unsigned char*)addr)[i]);
  475. }
  476. else
  477. {
  478. strcpy(dest+i*2, " ");
  479. }
  480. }
  481. }
  482. static void
  483. mr_to_char_dump(char* dest, void *addr, int size, int MaxBytes)
  484. {
  485. int i;
  486. char *c= (char*)addr;
  487. for(i=0;i<MaxBytes;i++)
  488. {
  489. if(i<size)
  490. {
  491. *(dest+i)= (isprint(*c)?*c:'.');
  492. c++;
  493. }
  494. else
  495. {
  496. *(dest+i)= ' ';
  497. }
  498. }
  499. *(dest+i)= '\0';
  500. }
  501. /*
  502. * Check that the address is (probably) valid
  503. */
  504. static int ValidateBP(UINT bp)
  505. {
  506. return !(IsBadReadPtr((void*)bp, 4) || IsBadWritePtr((void*)bp, 4));
  507. }
  508. /*
  509. * Check that the address is (probably) valid
  510. */
  511. static int ValidateIP(UINT ip)
  512. {
  513. return !IsBadReadPtr((void*)ip, 4);
  514. }
  515. static int
  516. memory_record_delete( caddr_t data, caddr_t arg )
  517. {
  518. struct memory_record *mr = (struct memory_record *)data;
  519. free(mr);
  520. return 0;
  521. }
  522. static int
  523. memory_record_duplicate_disallow( caddr_t d1, caddr_t d2 )
  524. {
  525. return -1;
  526. }
  527. static int
  528. memory_record_compare( caddr_t d1, caddr_t d2 )
  529. {
  530. struct memory_record *mr1 = (struct memory_record *)d1;
  531. struct memory_record *mr2 = (struct memory_record *)d2;
  532. return (mr1->p==mr2->p);
  533. }
  534. static void
  535. grab_stack(DWORD *ra,int framestograb,int framestoskip)
  536. {
  537. int framelookingat = 0;
  538. int framestoring = 0;
  539. DWORD _bp = 0;
  540. /* for every function the frame layout is:
  541. * ---------
  542. * |ret add|
  543. * ---------
  544. * |old bp | <- new bp
  545. * ---------
  546. */
  547. __asm mov _bp, ebp;
  548. if(framestoskip==0)
  549. {
  550. ra[framestoring]= _bp;
  551. framestoring++;
  552. }
  553. while (framelookingat < framestograb+framestoskip-1)
  554. {
  555. DWORD returnAddress = *(((DWORD*)_bp)+1);
  556. _bp = *((DWORD*)_bp);
  557. if (!ValidateBP(_bp)) break;
  558. if (!ValidateIP(returnAddress)) break;
  559. if(framelookingat>=framestoskip)
  560. {
  561. ra[framestoring]= returnAddress;
  562. framestoring++;
  563. }
  564. framelookingat++;
  565. }
  566. ra[framestoring]= 0;
  567. }
  568. static void
  569. add_memory_record(void *p,unsigned long size)
  570. {
  571. struct memory_record *mr= (struct memory_record *)malloc(sizeof(struct memory_record));
  572. mr->p= p;
  573. mr->size= size;
  574. grab_stack(mr->ra,MR_CALL_STACK,1);
  575. PR_Lock( mr_tree_lock );
  576. avl_insert( &mr_tree, mr, memory_record_compare, memory_record_duplicate_disallow );
  577. PR_Unlock( mr_tree_lock );
  578. }
  579. static void
  580. remove_memory_record(void *p)
  581. {
  582. struct memory_record *mr = NULL;
  583. struct memory_record search;
  584. PR_Lock( mr_tree_lock );
  585. search.p= p;
  586. mr = (struct memory_record *)avl_find( mr_tree, &search, memory_record_compare );
  587. if(mr!=NULL)
  588. {
  589. avl_delete( &mr_tree, mr, memory_record_compare );
  590. }
  591. PR_Unlock( mr_tree_lock );
  592. }
  593. #include <imagehlp.h>
  594. #pragma comment(lib, "imagehlp")
  595. static BOOL SymInitialized= FALSE;
  596. static HANDLE s_hProcess= NULL;
  597. BOOL InitialiseImageHelp()
  598. {
  599. if (!SymInitialized)
  600. {
  601. /* OBSOLETE: we don't have this directory structure any longer */
  602. /*
  603. * searchpath= <instancedir>\bin\slapd\server;<instancedir>\lib
  604. */
  605. char *searchpath= NULL;
  606. /* char *id= config_get_instancedir(); eliminated */
  607. if(id!=NULL)
  608. {
  609. char *p= id;
  610. while(p!=NULL)
  611. {
  612. p= strchr(id,'/');
  613. if(p!=NULL) *p='\\';
  614. }
  615. p= strrchr(id,'\\');
  616. if(p!=NULL)
  617. {
  618. *p= '\0';
  619. searchpath= slapi_ch_malloc(100+strlen(p)*2);
  620. strcpy(searchpath,id);
  621. strcat(searchpath,"\\bin\\slapd\\server;");
  622. strcat(searchpath,id);
  623. strcat(searchpath,"\\lib");
  624. }
  625. }
  626. s_hProcess = GetCurrentProcess();
  627. SymInitialized = SymInitialize(s_hProcess, searchpath, TRUE);
  628. slapi_ch_free((void**)&id);
  629. slapi_ch_free((void**)&searchpath);
  630. if (SymInitialized)
  631. {
  632. SymSetOptions(SYMOPT_DEFERRED_LOADS);
  633. }
  634. }
  635. return SymInitialized;
  636. }
  637. BOOL AddressToName(DWORD Addr, LPTSTR Str, int Max)
  638. {
  639. DWORD base;
  640. if (!InitialiseImageHelp())
  641. return FALSE;
  642. base = SymGetModuleBase(s_hProcess, Addr);
  643. if (base)
  644. {
  645. struct
  646. {
  647. IMAGEHLP_SYMBOL ihs;
  648. char NameBuf[256];
  649. } SymInfo;
  650. DWORD Displacement = 0;
  651. SymInfo.ihs.SizeOfStruct = sizeof(SymInfo);
  652. SymInfo.ihs.MaxNameLength = sizeof(SymInfo.NameBuf);
  653. if (SymGetSymFromAddr(s_hProcess, Addr, &Displacement, &SymInfo.ihs))
  654. {
  655. if (Displacement)
  656. _snprintf(Str, Max-1, "%s+%x", SymInfo.ihs.Name, Displacement);
  657. else
  658. _snprintf(Str, Max-1, "%s", SymInfo.ihs.Name);
  659. return TRUE;
  660. }
  661. else
  662. {
  663. _snprintf(Str, Max, "SymGetSymFromAddr failed (%d)", GetLastError());
  664. }
  665. }
  666. else
  667. {
  668. _snprintf(Str, Max, "SymGetModuleBase failed (%d)", GetLastError());
  669. }
  670. return FALSE;
  671. }
  672. static int
  673. memory_record_dump( caddr_t data, caddr_t arg )
  674. {
  675. int frame= 0;
  676. char b1[MR_DUMP_AMOUNT*2+1];
  677. char b2[MR_DUMP_AMOUNT+1];
  678. char b3[128];
  679. int size= 0;
  680. struct memory_record *mr = (struct memory_record *)data;
  681. if(!IsBadReadPtr(mr->p, MR_DUMP_AMOUNT))
  682. {
  683. size= MR_DUMP_AMOUNT;
  684. }
  685. mr_to_hex_dump(b1, mr->p, size, MR_DUMP_AMOUNT);
  686. mr_to_char_dump(b2, mr->p, size, MR_DUMP_AMOUNT);
  687. PR_snprintf(b3,sizeof(b3),"%p %ld %s %s",mr->p,mr->size,b1,b2);
  688. LDAPDebug( LDAP_DEBUG_ANY, "%s\n",b3,0,0);
  689. while(mr->ra[frame]!=0)
  690. {
  691. char fn[100];
  692. AddressToName(mr->ra[frame], fn, 100);
  693. LDAPDebug( LDAP_DEBUG_ANY, "%d %p %s\n",frame,mr->ra[frame],fn);
  694. frame++;
  695. }
  696. return 0;
  697. }
  698. #endif