From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1SPAKh-00051l-Vd for mharc-grub-devel@gnu.org; Tue, 01 May 2012 06:29:23 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54527) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SPAKd-00051M-2i for grub-devel@gnu.org; Tue, 01 May 2012 06:29:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SPAKY-0003fa-RT for grub-devel@gnu.org; Tue, 01 May 2012 06:29:18 -0400 Received: from mail-wi0-f171.google.com ([209.85.212.171]:48564) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SPAKY-0003bb-B8 for grub-devel@gnu.org; Tue, 01 May 2012 06:29:14 -0400 Received: by wibhj13 with SMTP id hj13so2663429wib.12 for ; Tue, 01 May 2012 03:29:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type; bh=DoLwzpUlWFGBT4/o5IMxHD5+yFkj1I/y0GTKncKm2MA=; b=DSO4KQrb9s9vGS045Wcl2LMxaUpYzdTrpGl/mbkJAs0HxQpLp+UqVAQYMms7MKyGLy G6Hj4bQ2TZOPDrTOFvAdNfmpO0TqZLYPt7IHWYNjP686XZ1Ty6E6S666CJ+wnbS0ZG+4 kFqwZVI2dDs7Y6Kw1Sp8OV/BsJkuqdoB8l+zY6cQaMgjP1staCoqyYzk7Bk+lc1rbfa1 JAoj+zJ1aSoKxK2F8dQYu3xP7Gves5/L8sGJcawwIYz/XJCC3JBTTgiL3GAii1MoZQad vz0JAM8vyPm4MKfpyKp1FwIzHDUwy/M2PRomYRBv3CVkL0l1XFZ9bnu4w9l0mPfOYmtL 6GXQ== Received: by 10.180.8.231 with SMTP id u7mr4139301wia.9.1335868152235; Tue, 01 May 2012 03:29:12 -0700 (PDT) Received: from debian.x201.phnet (95-232.197-178.cust.bluewin.ch. [178.197.232.95]) by mx.google.com with ESMTPS id k6sm35382007wie.9.2012.05.01.03.29.09 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 01 May 2012 03:29:10 -0700 (PDT) Message-ID: <4F9FBAEC.7080709@gmail.com> Date: Tue, 01 May 2012 12:29:00 +0200 From: =?UTF-8?B?VmxhZGltaXIgJ8+GLWNvZGVyL3BoY29kZXInIFNlcmJpbmVua28=?= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120329 Icedove/10.0.3 MIME-Version: 1.0 To: grub-devel@gnu.org Subject: Re: RFC: enhanced memory protection support References: <4F9ECACF.6050803@gmail.com> In-Reply-To: <4F9ECACF.6050803@gmail.com> X-Enigmail-Version: 1.4.1 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="------------enig4D1605BE5611C2B131877440" X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.212.171 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 May 2012 10:29:22 -0000 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig4D1605BE5611C2B131877440 Content-Type: multipart/mixed; boundary="------------020609070901010109050308" This is a multi-part message in MIME format. --------------020609070901010109050308 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 30.04.2012 19:24, Vladimir '=CF=86-coder/phcoder' Serbinenko wrote: > On 30.04.2012 17:26, Bean wrote: >> Hi, >> >> While testing network function in efi mode, I've found several memory >> leak related to fragmentation, but there is still some memory problem >> that's very tricky to locate. For example, you can run testspeed on a >> large file several times in a row and it could show the memory error. >> Since network condition are very difficult to reproduce, I have to >> look at the source of this issue, memory allocation. Currently it has >> mm_debug option that could print out file and line number of each >> memory call, but it's quite useless since we can't find the relevant >> informaton with full screen of prints. >> Here are some of my ideas for enhanced memory protection support: >> >> 1, when we allocate memory, we append some information at the end of >> the buffer, which include filename, lineno of caller, and tag to >> indicate what is used for and some padding to detect memory overwrite >> problem. >> >> 2. add a command to print the current memory list with extended >> information, this can be used to find memory leaks. >> >> 3. it's also a good idea to run the memory check in automated test to >> locate potential issue. > This is pretty easy to do leveraging some of the code I did for POSIX > support. But: > - Due to additional time and space required it should be done only when= > mm-debug is enabled. > - The additional data has to be stored before rather than after the > range since we store all the info before. > - Integrating with automated tests isn't that easy since some memory is= > intentionally never freed i.a. disk cache or otherwise in use (i.a. > terminal). We need exceptions for those. > I have half-working patch, just needs few fixes. > > Patch attached. It still has a bug since printing the report to terminal may change allocations and so iterator may become invalid. That's another reason why terminal should be excluded altogether from memory tracking. --=20 Regards Vladimir '=CF=86-coder/phcoder' Serbinenko --------------020609070901010109050308 Content-Type: text/x-diff; name="mm1.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="mm1.diff" =3D=3D=3D modified file 'config.h.in' --- config.h.in 2012-02-04 20:47:29 +0000 +++ config.h.in 2012-05-01 09:53:02 +0000 @@ -8,7 +8,11 @@ #if defined (GRUB_UTIL) || !defined (GRUB_MACHINE) #include #define NESTED_FUNC_ATTR +#define MM_DEBUG 0 #else + +/* Define to 1 if you enable memory manager debugging. */ +#define MM_DEBUG @MM_DEBUG@ /* Define if C symbols get an underscore after compilation. */ #define HAVE_ASM_USCORE @HAVE_ASM_USCORE@ /* Define it to \"addr32\" or \"addr32;\" to make GAS happy. */ =3D=3D=3D modified file 'configure.ac' --- configure.ac 2012-04-29 16:15:24 +0000 +++ configure.ac 2012-05-01 09:53:58 +0000 @@ -759,9 +759,14 @@ # Memory manager debugging. AC_ARG_ENABLE([mm-debug], AS_HELP_STRING([--enable-mm-debug], - [include memory manager debugging]), - [AC_DEFINE([MM_DEBUG], [1], - [Define to 1 if you enable memory manager debug= ging.])]) + [include memory manager debugging])) +if test x$enable_mm_debug =3D xyes; then + MM_DEBUG=3D1 +else + MM_DEBUG=3D0 +fi + +AC_SUBST([MM_DEBUG]) =20 AC_ARG_ENABLE([cache-stats], AS_HELP_STRING([--enable-cache-stats], @@ -786,6 +791,10 @@ [AS_HELP_STRING([--enable-grub-emu-pci], [build and install the `grub-emu' debugging= utility with PCI support (potentially dangerous) (default=3Dno)])]) =20 +if test "$platform" =3D emu && test "$MM_DEBUG" =3D 1; then + AC_MSG_ERROR([grub-emu doesn't support mm-debug]) +fi + if test "$platform" =3D emu; then missing_ncurses=3D [# Check for curses libraries.] @@ -1132,6 +1141,7 @@ AM_CONDITIONAL([COND_APPLE_CC], [test x$TARGET_APPLE_CC =3D x1]) AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu =3D xyes]) AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS =3D x= 1]) +AM_CONDITIONAL([COND_ENABLE_MM_DEBUG], [test x$MM_DEBUG =3D x1]) =20 AM_CONDITIONAL([COND_HAVE_ASM_USCORE], [test x$HAVE_ASM_USCORE =3D x1]) AM_CONDITIONAL([COND_CYGWIN], [test x$host_os =3D xcygwin]) =3D=3D=3D modified file 'grub-core/Makefile.core.def' --- grub-core/Makefile.core.def 2012-04-01 19:35:18 +0000 +++ grub-core/Makefile.core.def 2012-04-30 18:22:33 +0000 @@ -1816,6 +1816,13 @@ }; =20 module =3D { + name =3D printmem; + common =3D commands/printmem.c; + condition =3D COND_ENABLE_MM_DEBUG; + enable =3D noemu; +}; + +module =3D { name =3D adler32; common =3D lib/adler32.c; }; =3D=3D=3D added file 'grub-core/commands/printmem.c' --- grub-core/commands/printmem.c 1970-01-01 00:00:00 +0000 +++ grub-core/commands/printmem.c 2012-05-01 10:20:29 +0000 @@ -0,0 +1,75 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2012 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by= + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +static grub_err_t +grub_cmd_print_mem (grub_command_t ctxt __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + grub_mm_header_t cur; + for (cur =3D grub_mm_allocated; cur; cur =3D cur->next) + { + struct grub_mm_alloc_info *info =3D (struct grub_mm_alloc_info *) = (cur + 1); + if (grub_strcmp (info->file, "kern/dl.c") =3D=3D 0 + || grub_strcmp (info->file, "kern/env.c") =3D=3D 0 + || grub_strcmp (info->file, "kern/disk.c") =3D=3D 0 + || grub_strcmp (info->file, "kern/command.c") =3D=3D 0 + || grub_strcmp (info->file, "kern/parser.c") =3D=3D 0 + || grub_strcmp (info->file, "normal/dyncmd.c") =3D=3D 0 + || grub_strcmp (info->file, "normal/autofs.c") =3D=3D 0 + || grub_strcmp (info->file, "normal/crypto.c") =3D=3D 0 + || grub_strcmp (info->file, "normal/term.c") =3D=3D 0 + || grub_strcmp (info->file, "normal/color.c") =3D=3D 0 + || grub_strcmp (info->file, "script/script.c") =3D=3D 0 + || grub_strcmp (info->file, "script/argv.c") =3D=3D 0 + || grub_strcmp (info->file, "commands/extcmd.c") =3D=3D 0) + continue; + /* TRANSLATORS: This line lists currently allocated memory. + First two fiels are filename and line number. + It's followed by the pointer and the size. */ + grub_printf_ (N_("%s:%d %p %lu B\n"), info->file, info->lineno, cu= r + 1, + (unsigned long) + ((cur->size << GRUB_MM_ALIGN_LOG2) - sizeof (*cur) + - sizeof (*info))); + } + return 0; +} + +static grub_command_t cmd; + +GRUB_MOD_INIT(printmem) +{ + cmd =3D grub_register_command ("printmem", grub_cmd_print_mem, 0, + N_("Print currently used memory.")); +} + +GRUB_MOD_FINI(printmem) +{ + grub_unregister_command (cmd); +} =3D=3D=3D modified file 'grub-core/kern/misc.c' --- grub-core/kern/misc.c 2012-02-29 18:35:19 +0000 +++ grub-core/kern/misc.c 2012-05-01 10:08:34 +0000 @@ -436,22 +436,37 @@ return num; } =20 +#if MM_DEBUG +char * +grub_debug_strdup (const char *file, int line, const char *s) +#else char * grub_strdup (const char *s) +#endif { grub_size_t len; char *p; =20 len =3D grub_strlen (s) + 1; - p =3D (char *) grub_malloc (len); +#if MM_DEBUG + p =3D (char *) grub_debug_malloc (file, line, len); +#else + p =3D (char *) grub_malloc (len); =20 +#endif if (! p) return 0; =20 return grub_memcpy (p, s, len); } =20 + +#if MM_DEBUG +char * +grub_debug_strndup (const char *file, int line, const char *s, grub_size= _t n) +#else char * grub_strndup (const char *s, grub_size_t n) +#endif { grub_size_t len; char *p; @@ -459,7 +474,11 @@ len =3D grub_strlen (s); if (len > n) len =3D n; +#if MM_DEBUG + p =3D (char *) grub_debug_malloc (file, line, len + 1); +#else p =3D (char *) grub_malloc (len + 1); +#endif if (! p) return 0; =20 @@ -1051,8 +1070,13 @@ return ret; } =20 +#if MM_DEBUG +char * +grub_debug_xvasprintf (const char *file, int line, const char *fmt, va_l= ist ap) +#else char * grub_xvasprintf (const char *fmt, va_list ap) +#endif { grub_size_t s, as =3D PREALLOC_SIZE; char *ret; @@ -1060,7 +1084,11 @@ while (1) { va_list ap2; +#if MM_DEBUG + ret =3D grub_debug_malloc (file, line, as + 1); +#else ret =3D grub_malloc (as + 1); +#endif if (!ret) return NULL; =20 @@ -1078,14 +1106,23 @@ } } =20 +#if MM_DEBUG +char * +grub_debug_xasprintf (const char *file, int line, const char *fmt, ...) +#else char * grub_xasprintf (const char *fmt, ...) +#endif { va_list ap; char *ret; =20 va_start (ap, fmt); +#if MM_DEBUG + ret =3D grub_debug_xvasprintf (file, line, fmt, ap); +#else ret =3D grub_xvasprintf (fmt, ap); +#endif va_end (ap); =20 return ret; =3D=3D=3D modified file 'grub-core/kern/mm.c' --- grub-core/kern/mm.c 2012-02-03 13:20:31 +0000 +++ grub-core/kern/mm.c 2012-05-01 09:50:18 +0000 @@ -68,7 +68,7 @@ #include #include =20 -#ifdef MM_DEBUG +#if MM_DEBUG # undef grub_malloc # undef grub_zalloc # undef grub_realloc @@ -84,10 +84,13 @@ to the header and a pointer to its region, respectively. PTR must be allocated. */ static void -get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_= t *r) +get_header_from_pointer (void *ptr_in, grub_mm_header_t *p, grub_mm_regi= on_t *r) { - if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1)) - grub_fatal ("unaligned pointer %p", ptr); + void *ptr; + if ((grub_addr_t) ptr_in & (GRUB_MM_ALIGN - 1)) + grub_fatal ("unaligned pointer %p", ptr_in); + + ptr =3D (grub_mm_header_t) ptr_in - (GRUB_MM_CELLS_METADATA - 1); =20 for (*r =3D grub_mm_base; *r; *r =3D (*r)->next) if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1) @@ -95,11 +98,11 @@ break; =20 if (! *r) - grub_fatal ("out of range pointer %p", ptr); + grub_fatal ("out of range pointer %p", ptr_in); =20 *p =3D (grub_mm_header_t) ptr - 1; if ((*p)->magic !=3D GRUB_MM_ALLOC_MAGIC) - grub_fatal ("alloc magic is broken at %p", *p); + grub_fatal ("alloc magic is broken at %p: 0x%x", *p, (*p)->magic); } =20 /* Initialize a region starting from ADDR and whose size is SIZE, @@ -150,6 +153,9 @@ { grub_mm_header_t p, q; =20 + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header) =3D=3D GRUB_MM_ALI= GN); + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_region) =3D=3D GRUB_MM_ALI= GN); + /* When everything is allocated side effect is that *first will have a= lloc magic marked, meaning that there is no room in this region. */ if ((*first)->magic =3D=3D GRUB_MM_ALLOC_MAGIC) @@ -160,7 +166,8 @@ { grub_off_t extra; =20 - extra =3D ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) % align; + extra =3D ((grub_addr_t) (p + GRUB_MM_CELLS_METADATA) + >> GRUB_MM_ALIGN_LOG2) & (align - 1); if (extra) extra =3D align - extra; =20 @@ -266,7 +273,7 @@ pieces before this will be un-used. */ *first =3D q; =20 - return p + 1; + return p + GRUB_MM_CELLS_METADATA; } =20 /* Search was completed without result. */ @@ -282,7 +289,8 @@ grub_memalign (grub_size_t align, grub_size_t size) { grub_mm_region_t r; - grub_size_t n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) += 1; + grub_size_t n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + + GRUB_MM_CELLS_METADATA; int count =3D 0; =20 if (!grub_mm_base) @@ -435,7 +443,8 @@ } =20 /* FIXME: Not optimal. */ - n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1; + n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + + GRUB_MM_CELLS_METADATA; get_header_from_pointer (ptr, &p, &r); =20 if (p->size >=3D n) @@ -450,7 +459,7 @@ return q; } =20 -#ifdef MM_DEBUG +#if MM_DEBUG int grub_mm_debug =3D 0; =20 void @@ -510,6 +519,50 @@ grub_printf ("\n"); } =20 +grub_mm_header_t grub_mm_allocated; + +static void +save_ref (void *ptr, int line, const char *file) +{ + grub_mm_header_t head; + struct grub_mm_alloc_info *info; + + COMPILE_TIME_ASSERT ((sizeof (struct grub_mm_alloc_info) + & (GRUB_MM_ALIGN - 1)) =3D=3D 0); + + if (!ptr) + return; + + head =3D ptr; + head -=3D GRUB_MM_CELLS_METADATA; + info =3D (struct grub_mm_alloc_info *) (head + 1); + head->next =3D grub_mm_allocated; + if (head->next) + head->next->prev =3D head; + head->prev =3D 0; + info->lineno =3D line; + grub_strncpy (info->file, file, sizeof (info->file)); + grub_mm_allocated =3D head; +} + +static void +delete_ref (void *ptr) +{ + grub_mm_header_t head; + + if (!ptr) + return; + + head =3D ptr; + head -=3D GRUB_MM_CELLS_METADATA; + if (head->next) + head->next->prev =3D head->prev; + if (head->prev) + head->prev->next =3D head->next; + else if (grub_mm_allocated =3D=3D head) + grub_mm_allocated =3D head->next; +} + void * grub_debug_malloc (const char *file, int line, grub_size_t size) { @@ -518,6 +571,7 @@ if (grub_mm_debug) grub_printf ("%s:%d: malloc (0x%" PRIxGRUB_SIZE ") =3D ", file, line= , size); ptr =3D grub_malloc (size); + save_ref (ptr, line, file); if (grub_mm_debug) grub_printf ("%p\n", ptr); return ptr; @@ -531,6 +585,7 @@ if (grub_mm_debug) grub_printf ("%s:%d: zalloc (0x%" PRIxGRUB_SIZE ") =3D ", file, line= , size); ptr =3D grub_zalloc (size); + save_ref (ptr, line, file); if (grub_mm_debug) grub_printf ("%p\n", ptr); return ptr; @@ -539,6 +594,7 @@ void grub_debug_free (const char *file, int line, void *ptr) { + delete_ref (ptr); if (grub_mm_debug) grub_printf ("%s:%d: free (%p)\n", file, line, ptr); grub_free (ptr); @@ -547,9 +603,11 @@ void * grub_debug_realloc (const char *file, int line, void *ptr, grub_size_t s= ize) { + delete_ref (ptr); if (grub_mm_debug) grub_printf ("%s:%d: realloc (%p, 0x%" PRIxGRUB_SIZE ") =3D ", file,= line, ptr, size); ptr =3D grub_realloc (ptr, size); + save_ref (ptr, line, file); if (grub_mm_debug) grub_printf ("%p\n", ptr); return ptr; =3D=3D=3D modified file 'grub-core/lib/relocator.c' --- grub-core/lib/relocator.c 2012-04-13 14:55:20 +0000 +++ grub-core/lib/relocator.c 2012-05-01 09:34:38 +0000 @@ -358,7 +358,9 @@ r2->first =3D r1->first; hl->next =3D r2->first; *rp =3D (*rp)->next; - grub_free (g + 1); + g->next =3D 0; + g->prev =3D 0; + grub_free (g + GRUB_MM_CELLS_METADATA); } break; } @@ -368,10 +370,11 @@ GRUB_MM_ALIGN); h->size =3D ((subchu->start + subchu->size + GRUB_MM_ALIGN - 1) / GRUB_MM_ALI= GN) - - (subchu->start / GRUB_MM_ALIGN) - 1; - h->next =3D h; + - (subchu->start / GRUB_MM_ALIGN); + h->next =3D 0; + h->prev =3D 0; h->magic =3D GRUB_MM_ALLOC_MAGIC; - grub_free (h + 1); + grub_free (h + GRUB_MM_CELLS_METADATA); break; } #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS @@ -428,6 +431,9 @@ unsigned j, N =3D 0; grub_addr_t target =3D 0; =20 + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header) =3D=3D GRUB_MM_ALI= GN); + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_region) =3D=3D GRUB_MM_ALI= GN); + grub_dprintf ("relocator", "trying to allocate in 0x%lx-0x%lx aligned 0x%lx size 0x%lx\n", (unsigned long) start, (unsigned long) end, =3D=3D=3D modified file 'grub-core/normal/main.c' --- grub-core/normal/main.c 2012-03-11 13:43:18 +0000 +++ grub-core/normal/main.c 2012-05-01 10:16:23 +0000 @@ -41,8 +41,13 @@ int grub_normal_exit_level =3D 0; =20 /* Read a line from the file FILE. */ +#if MM_DEBUG +char * +grub_file_debug_getline (const char *cfile, int line, grub_file_t file) +#else char * grub_file_getline (grub_file_t file) +#endif { char c; grub_size_t pos =3D 0; @@ -51,7 +56,11 @@ grub_size_t max_len =3D 64; =20 /* Initially locate some space. */ - cmdline =3D grub_malloc (max_len); +#if MM_DEBUG + cmdline =3D grub_debug_malloc (cfile, line, max_len); +#else + cmdline =3D grub_debug_malloc (max_len); +#endif if (! cmdline) return 0; =20 @@ -69,7 +78,11 @@ { char *old_cmdline =3D cmdline; max_len =3D max_len * 2; +#if MM_DEBUG + cmdline =3D grub_debug_realloc (cfile, line, cmdline, max_len); +#else cmdline =3D grub_realloc (cmdline, max_len); +#endif if (! cmdline) { grub_free (old_cmdline); =3D=3D=3D modified file 'include/grub/misc.h' --- include/grub/misc.h 2012-03-05 00:17:55 +0000 +++ include/grub/misc.h 2012-05-01 10:07:51 +0000 @@ -314,8 +314,16 @@ } } =20 +#if MM_DEBUG +char *EXPORT_FUNC(grub_debug_strdup) (const char *file, int line, const = char *s) __attribute__ ((warn_unused_result)); +char *EXPORT_FUNC(grub_debug_strndup) (const char *file, int line, const= char *s, grub_size_t n) __attribute__ ((warn_unused_result)); + +#define grub_strdup(s) grub_debug_strdup (GRUB_FILE, __LINE__, s) +#define grub_strndup(s, n) grub_debug_strndup (GRUB_FILE, __LINE__, s, n= ) +#else char *EXPORT_FUNC(grub_strdup) (const char *s) __attribute__ ((warn_unus= ed_result)); char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n) __attribu= te__ ((warn_unused_result)); +#endif void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n); grub_size_t EXPORT_FUNC(grub_strlen) (const char *s) __attribute__ ((war= n_unused_result)); int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((form= at (printf, 1, 2))); @@ -363,9 +371,17 @@ __attribute__ ((format (printf, 3, 4))); int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *f= mt, va_list args); +#if MM_DEBUG +char *EXPORT_FUNC(grub_debug_xasprintf) (const char *file, int line, con= st char *fmt, ...) + __attribute__ ((format (printf, 3, 4))) __attribute__ ((warn_unused= _result)); +char *EXPORT_FUNC(grub_debug_xvasprintf) (const char *file, int line, co= nst char *fmt, va_list args) __attribute__ ((warn_unused_result)); +#define grub_xasprintf(fmt, args...) grub_debug_xasprintf (GRUB_FILE, __= LINE__, fmt, ## args) +#define grub_xvasprintf(fmt, args) grub_debug_xvasprintf (GRUB_FILE, __L= INE__, fmt, args) +#else char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))) __attribute__ ((warn_unused= _result)); char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) __att= ribute__ ((warn_unused_result)); +#endif void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn)); void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn)); grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n, =3D=3D=3D modified file 'include/grub/mm.h' --- include/grub/mm.h 2011-11-13 11:48:39 +0000 +++ include/grub/mm.h 2012-05-01 09:53:09 +0000 @@ -39,7 +39,7 @@ #define grub_mm_check() grub_mm_check_real (GRUB_FILE, __LINE__); =20 /* For debugging. */ -#if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_E= MU) +#if MM_DEBUG /* Set this variable to 1 when you want to trace all memory function cal= ls. */ extern int EXPORT_VAR(grub_mm_debug); =20 =3D=3D=3D modified file 'include/grub/mm_private.h' --- include/grub/mm_private.h 2010-08-28 12:52:25 +0000 +++ include/grub/mm_private.h 2012-05-01 09:46:58 +0000 @@ -30,13 +30,7 @@ struct grub_mm_header *next; grub_size_t size; grub_size_t magic; -#if GRUB_CPU_SIZEOF_VOID_P =3D=3D 4 - char padding[4]; -#elif GRUB_CPU_SIZEOF_VOID_P =3D=3D 8 - char padding[8]; -#else -# error "unknown word size" -#endif + struct grub_mm_header *prev; } *grub_mm_header_t; =20 @@ -48,6 +42,17 @@ =20 #define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2) =20 +#if MM_DEBUG +struct grub_mm_alloc_info +{ + int lineno; + char file[128 - sizeof (int)]; +}; +static const grub_size_t GRUB_MM_CELLS_METADATA =3D ((sizeof (struct gru= b_mm_alloc_info) >> GRUB_MM_ALIGN_LOG2) + 1); +#else +#define GRUB_MM_CELLS_METADATA 1 +#endif + typedef struct grub_mm_region { struct grub_mm_header *first; @@ -61,4 +66,8 @@ extern grub_mm_region_t EXPORT_VAR (grub_mm_base); #endif =20 +#if MM_DEBUG +extern grub_mm_header_t EXPORT_VAR(grub_mm_allocated); +#endif + #endif =3D=3D=3D modified file 'include/grub/normal.h' --- include/grub/normal.h 2012-03-10 22:25:34 +0000 +++ include/grub/normal.h 2012-05-01 10:15:01 +0000 @@ -51,7 +51,14 @@ void grub_menu_init_page (int nested, int edit, int *num_entries, struct grub_term_output *term); void grub_normal_init_page (struct grub_term_output *term); +#if MM_DEBUG +char *grub_file_debug_getline (const char *call_file, + int line, grub_file_t file); +#define grub_file_getline(file) \ + grub_file_debug_getline (GRUB_FILE, __LINE__, file) +#else char *grub_file_getline (grub_file_t file); +#endif void grub_cmdline_run (int nested); =20 /* Defined in `cmdline.c'. */ --------------020609070901010109050308-- --------------enig4D1605BE5611C2B131877440 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iF4EAREKAAYFAk+fuvMACgkQNak7dOguQgm0twD+N28Ld8UdBu2LltrireH8MSd0 fUg3r0eiMqRvDSJ2zHYBALYLti7dXTITEM3aBwZzDGW/rZPa6YrTui2QdqSPb2ns =t92x -----END PGP SIGNATURE----- --------------enig4D1605BE5611C2B131877440--