From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 81492 invoked by alias); 2 May 2017 13:15:54 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 81443 invoked by uid 89); 2 May 2017 13:15:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00,FREEMAIL_FROM,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=ham version=3.3.2 spammy=TARGET, MOD X-HELO: mail-wm0-f65.google.com Received: from mail-wm0-f65.google.com (HELO mail-wm0-f65.google.com) (74.125.82.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 02 May 2017 13:15:48 +0000 Received: by mail-wm0-f65.google.com with SMTP id d79so4451890wmi.2 for ; Tue, 02 May 2017 06:15:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=h8CRWMZtK5RwjY9bGHhR+bRNUzjgf+xsNjXOsrx7GDM=; b=Nsv9KOa60uciY8wOdkwB3JfaEKM7yYQyiXjGyUVn/p2a3Sa/nCEOoBFs7RRtNAD4em dShux0KO2gawbk+WqmT5le8nYhQ/xonjEO+3SVxgqdGC0hnCztKTZOFgNKlNneLnhQCP V3zgXuyVlKB0I3FAaCqWCU06schRAIVDtdFwq+D9s1WIwWNGD1rubBZp+zHu5i4bdCzK om0+wTEAuetOztYiVhPAJdp+Az9C7S2kHMAEKxjBXhi7AQh9sgAKRBaYqhkyIsxbgABj /wEG7drSHg1ZReFcaIZjvCLxgFqdHAFJKh4HADv21im/OCa1x0ca8Nb3/D4DEhhML20e Ciqw== X-Gm-Message-State: AN3rC/4STwryryH0TNQ3fLljRjYr+TpKnIQnq/PmI6q6UuYp8njf6zgp DVa6ICYb6fZoWQ== X-Received: by 10.28.45.212 with SMTP id t203mr2546259wmt.99.1493730948523; Tue, 02 May 2017 06:15:48 -0700 (PDT) Received: from E107787-LIN ([194.214.185.158]) by smtp.gmail.com with ESMTPSA id r29sm16323879wra.18.2017.05.02.06.15.47 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 02 May 2017 06:15:47 -0700 (PDT) From: Yao Qi To: Philipp Rudo Cc: gdb-patches@sourceware.org, Yao Qi , Peter Griffin , Omair Javaid , Andreas Arnez Subject: Re: [RFC v3 4/8] Add kernel module support for linux-kernel target References: <20170316165739.88524-1-prudo@linux.vnet.ibm.com> <20170316165739.88524-5-prudo@linux.vnet.ibm.com> Date: Tue, 02 May 2017 13:15:00 -0000 In-Reply-To: <20170316165739.88524-5-prudo@linux.vnet.ibm.com> (Philipp Rudo's message of "Thu, 16 Mar 2017 17:57:35 +0100") Message-ID: <868tmf8kn4.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2017-05/txt/msg00005.txt.bz2 Philipp Rudo writes: > +/* Translate a kernel virtual address ADDR to a physical address. */ > + > +CORE_ADDR > +lk_kvtop (CORE_ADDR addr) How about lk_kernel_vir_to_phy_addr? > +{ > + CORE_ADDR pgd =3D lk_read_addr (LK_ADDR (init_mm) > + + LK_OFFSET (mm_struct, pgd)); > + return LK_HOOK->vtop (pgd, addr); > +} > + > +/* Restore current_target to TARGET. */ > +static void > +restore_current_target (void *target) > +{ > + current_target.beneath =3D (struct target_ops *) target; > +} > + > +/* Function for targets to_xfer_partial hook. */ > + > +enum target_xfer_status > +lk_xfer_partial (struct target_ops *ops, enum target_object object, > + const char *annex, gdb_byte *readbuf, > + const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, > + ULONGEST *xfered_len) > +{ > + enum target_xfer_status ret_val; > + struct cleanup *old_chain =3D make_cleanup (restore_current_target, > ops); Use make_scoped_restore instead of make_cleanup? > + > + current_target.beneath =3D ops->beneath; > + Any reasons you switch current_target.beneath temporarily? > + if (LK_HOOK->is_kvaddr (offset)) > + offset =3D lk_kvtop (offset); > + > + ret_val =3D ops->beneath->to_xfer_partial (ops->beneath, object, anne= x, > + readbuf, writebuf, offset, len, > + xfered_len); Two spaces after "=3D". > diff --git a/gdb/lk-modules.c b/gdb/lk-modules.c > new file mode 100644 > index 0000000..f3c559d > --- /dev/null > +++ b/gdb/lk-modules.c > @@ -0,0 +1,412 @@ > +/* Handle Linux kernel modules as shared libraries. > + > + Copyright (C) 2016 Free Software Foundation, Inc. > + > + This file is part of GDB. > + > + This program 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. > + > + This program 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 this program. If not, see .= */ > + > +#include "defs.h" > + > +#include "common/filestuff.h" > +#include "filenames.h" > +#include "gdbcmd.h" > +#include "gdbcore.h" > +#include "gdb_regex.h" > +#include "lk-lists.h" > +#include "lk-low.h" > +#include "lk-modules.h" > +#include "objfiles.h" > +#include "observer.h" > +#include "readline/readline.h" > +#include "solib.h" > +#include "solist.h" > +#include "utils.h" > + > +#include > +#include > + > +struct target_so_ops *lk_modules_so_ops =3D NULL; > + > +/* Info for single section type. */ > + > +struct lm_info_sec > +{ > + CORE_ADDR start; > + CORE_ADDR offset; > + unsigned int size; > +}; > + > +/* Link map info to include in an allocated so_list entry. */ > + > +struct lm_info > +{ > + CORE_ADDR base; > + unsigned int size; > + > + struct lm_info_sec text; > + struct lm_info_sec init_text; > + struct lm_info_sec ro_data; > + struct lm_info_sec rw_data; > + struct lm_info_sec percpu; > +}; Comments to these fields are needed. > + > +/* Build map between module name and path to binary file by reading file > + modules.order. Returns unordered_map with module name as key and its > + path as value. */ > + > +std::unordered_map > +lk_modules_build_path_map () > +{ > + std::unordered_map umap; > + FILE *mod_order; > + struct cleanup *old_chain; > + char line[SO_NAME_MAX_PATH_SIZE + 1]; > + > + mod_order =3D lk_modules_open_mod_order (); > + old_chain =3D make_cleanup_fclose (mod_order); > + > + line[SO_NAME_MAX_PATH_SIZE] =3D '\0'; > + std::string search_path =3D lk_modules_expand_search_path (); > + while (fgets (line, SO_NAME_MAX_PATH_SIZE, mod_order)) > + { > + /* Remove trailing newline. */ > + line[strlen (line) - 1] =3D '\0'; > + > + std::string name =3D lbasename (line); > + > + /* 3 =3D strlen (".ko"). */ > + if (!endswith (name.c_str (), ".ko") > + || name.length () >=3D LK_MODULE_NAME_LEN + 3) > + continue; > + > + name =3D name.substr (0, name.length () - 3); > + > + /* Kernel modules are named after the files they are stored in with > + all minus '-' replaced by underscore '_'. Do the same to enable > + mapping. */ > + for (size_t p =3D name.find('-'); p !=3D std::string::npos; > + p =3D name.find ('-', p + 1)) > + name[p] =3D '_'; > + > + umap[name] =3D concat_path(search_path, line); > + } > + > + do_cleanups (old_chain); > + return umap; > +} > + > +/* Allocate and fill a copy of struct lm_info for module at address MOD.= */ > + > +struct lm_info * > +lk_modules_read_lm_info (CORE_ADDR mod) > +{ > + struct lm_info *lmi =3D XNEW (struct lm_info); > + struct cleanup *old_chain =3D make_cleanup (xfree, lmi); > + use std::unique_ptr to avoid cleanup. sdt::unique_ptr lmi (new lm_info ()); > + if (LK_FIELD (module, module_core)) /* linux -4.4 */ > + { > + lmi->base =3D lk_read_addr (mod + LK_OFFSET (module, module_core)); > + lmi->size =3D lk_read_addr (mod + LK_OFFSET (module, core_size)); > + > + lmi->text.start =3D lmi->base; > + lmi->text.offset =3D LK_HOOK->get_module_text_offset (mod); > + lmi->text.size =3D lk_read_uint (mod + LK_OFFSET (module, core_tex= t_size)); > + > + lmi->ro_data.start =3D lmi->base + lmi->text.size; > + lmi->ro_data.offset =3D 0; > + lmi->ro_data.size =3D lk_read_uint (mod + LK_OFFSET (module, > + core_ro_size)); > + } > + else /* linux 4.5+ */ > + { > + CORE_ADDR mod_core =3D mod + LK_OFFSET (module, core_layout); > + > + lmi->base =3D lk_read_addr (mod_core > + + LK_OFFSET (module_layout, base)); > + lmi->size =3D lk_read_uint (mod_core > + + LK_OFFSET (module_layout, size)); > + > + lmi->text.start =3D lmi->base; > + lmi->text.offset =3D LK_HOOK->get_module_text_offset (mod); > + lmi->text.size =3D lk_read_uint (mod_core > + + LK_OFFSET (module_layout, text_size)); > + > + lmi->ro_data.start =3D lmi->base + lmi->text.size; > + lmi->ro_data.offset =3D 0; > + lmi->ro_data.size =3D lk_read_uint (mod_core > + + LK_OFFSET (module_layout, ro_size)); > + } > + > + lmi->rw_data.start =3D lmi->base + lmi->ro_data.size; > + lmi->rw_data.offset =3D 0; > + lmi->rw_data.size =3D lmi->size - lmi->ro_data.size; > + > + lmi->init_text.start =3D lk_read_addr (mod + LK_OFFSET (module, init)); > + lmi->init_text.offset =3D 0; > + > + lmi->percpu.start =3D lk_read_addr (mod + LK_OFFSET (module, percpu)); > + lmi->percpu.size =3D lk_read_uint (mod + LK_OFFSET (module, percpu_siz= e)); > + lmi->percpu.offset =3D 0; > + > + discard_cleanups (old_chain); > + return lmi; > +} > + --=20 Yao (=E9=BD=90=E5=B0=A7)