ch_malloc.c 18 KB

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