From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30165 invoked by alias); 11 Mar 2004 17:36:50 -0000 Mailing-List: contact gdb-patches-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sources.redhat.com Received: (qmail 30156 invoked from network); 11 Mar 2004 17:36:49 -0000 Received: from unknown (HELO mx1.redhat.com) (66.187.233.31) by sources.redhat.com with SMTP; 11 Mar 2004 17:36:49 -0000 Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.12.10/8.12.10) with ESMTP id i2BHan07015152 for ; Thu, 11 Mar 2004 12:36:49 -0500 Received: from pobox.corp.redhat.com (pobox.corp.redhat.com [172.16.52.156]) by int-mx1.corp.redhat.com (8.11.6/8.11.6) with ESMTP id i2BHan820621 for ; Thu, 11 Mar 2004 12:36:49 -0500 Received: from localhost.localdomain (vpn50-70.rdu.redhat.com [172.16.50.70]) by pobox.corp.redhat.com (8.12.8/8.12.8) with ESMTP id i2BHamE3000804 for ; Thu, 11 Mar 2004 12:36:48 -0500 Received: from saguaro (saguaro.lan [192.168.64.2]) by localhost.localdomain (8.12.10/8.12.10) with SMTP id i2BHahcG027625 for ; Thu, 11 Mar 2004 10:36:43 -0700 Date: Thu, 11 Mar 2004 17:36:00 -0000 From: Kevin Buettner To: gdb-patches@sources.redhat.com Subject: [PATCH] Fix memory leak in solib-svr4.c Message-Id: <20040311103643.4a4e170b@saguaro> Organization: Red Hat Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-SW-Source: 2004-03.o/txt/msg00273.txt I've just committed the patch below. It fixes a memory leak in solib-svr4.c. In enable_break(), the memory allocated by svr4_current_sos() was never being freed. I could have iterated over the list returned by this function and freed each entry, but, instead, I chose to eliminate the call to svr4_current_sos() altogether. It appears to me that the call to svr4_current_sos() was being used for two purposes: 1) to determine whether or not to call solib_add(), and 2) as a means for finding the dynamic linker's base address. solib_add() makes its own call to svr4_current_sos(), which means that svr4_current_sos() was being invoked an extra time for no good reason (that I can see). The code has been reorganized to instead use the master list operated on by solib_add(). Kevin * solist.h (master_so_list): New function. * solib.c (master_so_list): Likewise. * solib-svr4.c (enable_break): Iterate over so_list entries obtained from master list instead of entries obtained directly via svr4_current_sos(). Index: solib-svr4.c =================================================================== RCS file: /cvs/src/src/gdb/solib-svr4.c,v retrieving revision 1.41 diff -c -p -r1.41 solib-svr4.c *** solib-svr4.c 21 Feb 2004 18:34:45 -0000 1.41 --- solib-svr4.c 11 Mar 2004 16:57:35 -0000 *************** enable_break (void) *** 1004,1010 **** char *buf; CORE_ADDR load_addr = 0; int load_addr_found = 0; ! struct so_list *inferior_sos; bfd *tmp_bfd = NULL; struct target_ops *tmp_bfd_target; int tmp_fd = -1; --- 1004,1010 ---- char *buf; CORE_ADDR load_addr = 0; int load_addr_found = 0; ! struct so_list *so; bfd *tmp_bfd = NULL; struct target_ops *tmp_bfd_target; int tmp_fd = -1; *************** enable_break (void) *** 1047,1069 **** target will also close the underlying bfd. */ tmp_bfd_target = target_bfd_reopen (tmp_bfd); ! /* If the entry in _DYNAMIC for the dynamic linker has already ! been filled in, we can read its base address from there. */ ! inferior_sos = svr4_current_sos (); ! if (inferior_sos) { ! /* Connected to a running target. Update our shared library table. */ ! solib_add (NULL, 0, NULL, auto_solib_add); ! } ! while (inferior_sos) ! { ! if (strcmp (buf, inferior_sos->so_original_name) == 0) { load_addr_found = 1; ! load_addr = LM_ADDR (inferior_sos); break; } ! inferior_sos = inferior_sos->next; } /* Otherwise we find the dynamic linker's base address by examining --- 1047,1065 ---- target will also close the underlying bfd. */ tmp_bfd_target = target_bfd_reopen (tmp_bfd); ! /* On a running target, we can get the dynamic linker's base ! address from the shared library table. */ ! solib_add (NULL, 0, NULL, auto_solib_add); ! so = master_so_list (); ! while (so) { ! if (strcmp (buf, so->so_original_name) == 0) { load_addr_found = 1; ! load_addr = LM_ADDR (so); break; } ! so = so->next; } /* Otherwise we find the dynamic linker's base address by examining Index: solib.c =================================================================== RCS file: /cvs/src/src/gdb/solib.c,v retrieving revision 1.63 diff -c -p -r1.63 solib.c *** solib.c 28 Feb 2004 18:04:37 -0000 1.63 --- solib.c 11 Mar 2004 16:57:35 -0000 *************** free_so (struct so_list *so) *** 333,338 **** --- 333,346 ---- } + /* Return address of first so_list entry in master shared object list. */ + struct so_list * + master_so_list (void) + { + return so_list_head; + } + + /* A small stub to get us past the arg-passing pinhole of catch_errors. */ static int Index: solist.h =================================================================== RCS file: /cvs/src/src/gdb/solist.h,v retrieving revision 1.8 diff -c -p -r1.8 solist.h *** solist.h 24 Feb 2003 19:11:04 -0000 1.8 --- solist.h 11 Mar 2004 16:57:35 -0000 *************** struct target_so_ops *** 107,113 **** --- 107,117 ---- }; + /* Free the memory associated with a (so_list *). */ void free_so (struct so_list *so); + + /* Return address of first so_list entry in master shared object list. */ + struct so_list *master_so_list (void); /* Find solib binary file and open it. */ extern int solib_open (char *in_pathname, char **found_pathname); From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30165 invoked by alias); 11 Mar 2004 17:36:50 -0000 Mailing-List: contact gdb-patches-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sources.redhat.com Received: (qmail 30156 invoked from network); 11 Mar 2004 17:36:49 -0000 Received: from unknown (HELO mx1.redhat.com) (66.187.233.31) by sources.redhat.com with SMTP; 11 Mar 2004 17:36:49 -0000 Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.12.10/8.12.10) with ESMTP id i2BHan07015152 for ; Thu, 11 Mar 2004 12:36:49 -0500 Received: from pobox.corp.redhat.com (pobox.corp.redhat.com [172.16.52.156]) by int-mx1.corp.redhat.com (8.11.6/8.11.6) with ESMTP id i2BHan820621 for ; Thu, 11 Mar 2004 12:36:49 -0500 Received: from localhost.localdomain (vpn50-70.rdu.redhat.com [172.16.50.70]) by pobox.corp.redhat.com (8.12.8/8.12.8) with ESMTP id i2BHamE3000804 for ; Thu, 11 Mar 2004 12:36:48 -0500 Received: from saguaro (saguaro.lan [192.168.64.2]) by localhost.localdomain (8.12.10/8.12.10) with SMTP id i2BHahcG027625 for ; Thu, 11 Mar 2004 10:36:43 -0700 Date: Fri, 19 Mar 2004 00:09:00 -0000 From: Kevin Buettner To: gdb-patches@sources.redhat.com Subject: [PATCH] Fix memory leak in solib-svr4.c Message-ID: <20040311103643.4a4e170b@saguaro> Organization: Red Hat Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-SW-Source: 2004-03/txt/msg00273.txt.bz2 Message-ID: <20040319000900.Ri4a1m6343Mt_plfFMkvg_kxRW67K3jkNgS2DIA832A@z> I've just committed the patch below. It fixes a memory leak in solib-svr4.c. In enable_break(), the memory allocated by svr4_current_sos() was never being freed. I could have iterated over the list returned by this function and freed each entry, but, instead, I chose to eliminate the call to svr4_current_sos() altogether. It appears to me that the call to svr4_current_sos() was being used for two purposes: 1) to determine whether or not to call solib_add(), and 2) as a means for finding the dynamic linker's base address. solib_add() makes its own call to svr4_current_sos(), which means that svr4_current_sos() was being invoked an extra time for no good reason (that I can see). The code has been reorganized to instead use the master list operated on by solib_add(). Kevin * solist.h (master_so_list): New function. * solib.c (master_so_list): Likewise. * solib-svr4.c (enable_break): Iterate over so_list entries obtained from master list instead of entries obtained directly via svr4_current_sos(). Index: solib-svr4.c =================================================================== RCS file: /cvs/src/src/gdb/solib-svr4.c,v retrieving revision 1.41 diff -c -p -r1.41 solib-svr4.c *** solib-svr4.c 21 Feb 2004 18:34:45 -0000 1.41 --- solib-svr4.c 11 Mar 2004 16:57:35 -0000 *************** enable_break (void) *** 1004,1010 **** char *buf; CORE_ADDR load_addr = 0; int load_addr_found = 0; ! struct so_list *inferior_sos; bfd *tmp_bfd = NULL; struct target_ops *tmp_bfd_target; int tmp_fd = -1; --- 1004,1010 ---- char *buf; CORE_ADDR load_addr = 0; int load_addr_found = 0; ! struct so_list *so; bfd *tmp_bfd = NULL; struct target_ops *tmp_bfd_target; int tmp_fd = -1; *************** enable_break (void) *** 1047,1069 **** target will also close the underlying bfd. */ tmp_bfd_target = target_bfd_reopen (tmp_bfd); ! /* If the entry in _DYNAMIC for the dynamic linker has already ! been filled in, we can read its base address from there. */ ! inferior_sos = svr4_current_sos (); ! if (inferior_sos) { ! /* Connected to a running target. Update our shared library table. */ ! solib_add (NULL, 0, NULL, auto_solib_add); ! } ! while (inferior_sos) ! { ! if (strcmp (buf, inferior_sos->so_original_name) == 0) { load_addr_found = 1; ! load_addr = LM_ADDR (inferior_sos); break; } ! inferior_sos = inferior_sos->next; } /* Otherwise we find the dynamic linker's base address by examining --- 1047,1065 ---- target will also close the underlying bfd. */ tmp_bfd_target = target_bfd_reopen (tmp_bfd); ! /* On a running target, we can get the dynamic linker's base ! address from the shared library table. */ ! solib_add (NULL, 0, NULL, auto_solib_add); ! so = master_so_list (); ! while (so) { ! if (strcmp (buf, so->so_original_name) == 0) { load_addr_found = 1; ! load_addr = LM_ADDR (so); break; } ! so = so->next; } /* Otherwise we find the dynamic linker's base address by examining Index: solib.c =================================================================== RCS file: /cvs/src/src/gdb/solib.c,v retrieving revision 1.63 diff -c -p -r1.63 solib.c *** solib.c 28 Feb 2004 18:04:37 -0000 1.63 --- solib.c 11 Mar 2004 16:57:35 -0000 *************** free_so (struct so_list *so) *** 333,338 **** --- 333,346 ---- } + /* Return address of first so_list entry in master shared object list. */ + struct so_list * + master_so_list (void) + { + return so_list_head; + } + + /* A small stub to get us past the arg-passing pinhole of catch_errors. */ static int Index: solist.h =================================================================== RCS file: /cvs/src/src/gdb/solist.h,v retrieving revision 1.8 diff -c -p -r1.8 solist.h *** solist.h 24 Feb 2003 19:11:04 -0000 1.8 --- solist.h 11 Mar 2004 16:57:35 -0000 *************** struct target_so_ops *** 107,113 **** --- 107,117 ---- }; + /* Free the memory associated with a (so_list *). */ void free_so (struct so_list *so); + + /* Return address of first so_list entry in master shared object list. */ + struct so_list *master_so_list (void); /* Find solib binary file and open it. */ extern int solib_open (char *in_pathname, char **found_pathname);