From patchwork Sun Nov 18 20:19:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 999547 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=chromium.org Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42yjxt6Msqz9s3Z for ; Mon, 19 Nov 2018 07:20:02 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 9C485C22431; Sun, 18 Nov 2018 20:19:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 3EB39C21F45; Sun, 18 Nov 2018 20:19:54 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 5C77FC21F45; Sun, 18 Nov 2018 20:19:52 +0000 (UTC) Received: from mail-vs1-f74.google.com (mail-vs1-f74.google.com [209.85.217.74]) by lists.denx.de (Postfix) with ESMTPS id CE610C21D56 for ; Sun, 18 Nov 2018 20:19:51 +0000 (UTC) Received: by mail-vs1-f74.google.com with SMTP id j123so13485272vsd.9 for ; Sun, 18 Nov 2018 12:19:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=6m2k0f79inx6dIczd9fZFovHWgKrDh4Bo+7S2Sx+AF8=; b=PzxGrhsGSF2i482xRKcG5yXqMRXolmTJfW/31qIJniWD9t/m51CMqRqSRvqxVzO+Q8 CJjGpGGliQwOVd4O5mmjKYJpfUc/VWCcNVBq6HtE23EMms0G32dAYBw9KFxA2VTBjGHj G9ffllvbn+QBh+mRzZS2P8nKGBCjuz7r35xYg8r2I1E6AzLaIZyDQrhscOvKvYBTUxd+ DZExidRGRpma3EM59dfxTrLg3e01Y8cecQsmqRAwqqQQn1DOg/Iy4/0XbT4/1IQUI23E HvOMLrXQZm3MAYVSgTOmDz4aIWEinBQWmff9R1AGM2UMydgMxgHw0giXvuQI/Poze2Tb /9jQ== X-Gm-Message-State: AGRZ1gLr8m22bxksqYxghvAkCfVK8tLTkcjvScAbWGWMk0ovOFHejTJO piPHCCcnOLWZYvUltKWhGdM+ZVw= X-Google-Smtp-Source: AJdET5cETV6MZwjPuaN9sX6jL5vn5CfGkjFRDm868Wb+pk3spr6FORJ70jh6hlzugdrHklfdYonvw38= X-Received: by 2002:a1f:a78c:: with SMTP id q134mr11499343vke.6.1542572390700; Sun, 18 Nov 2018 12:19:50 -0800 (PST) Date: Sun, 18 Nov 2018 13:19:48 -0700 Message-Id: <20181118201948.191322-1-sjg@chromium.org> Mime-Version: 1.0 X-Mailer: git-send-email 2.19.1.1215.g8438c0b245-goog From: Simon Glass To: U-Boot Mailing List Cc: Alexey Brodkin , Alexander Graf Subject: [U-Boot] [PATCH] sandbox: Improve debugging in initcall_run_list() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" At present if one of the initcalls fails on sandbox the address printing is not help, e.g.: initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96) This is because U-Boot gets relocated high into memory and the relocation offset (gd->reloc_off) does not work correctly for sandbox. Add support for finding the base address of the text region (at least on Linux) and use that to set the relocation offset. This makes the output better: initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96) Then you use can use grep to see which init call failed, e.g.: $ grep 0000000000048134 u-boot.map stdio_add_devices Of course another option is to run it with a debugger such as gdb: $ gdb u-boot ... (gdb) br initcall.c:32 Breakpoint 1 at 0x5555555e388f: file lib/initcall.c, line 32. (gdb) r Starting program: /tmp/b/sandbox/u-boot [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600) DRAM: 128 MiB MMC: Breakpoint 1, initcall_run_list (init_sequence=init_sequence@entry=0x5555558b5c80 ) at lib/initcall.c:32 32 printf("initcall sequence %p failed at call %p (err=%d)\n", (gdb) print *init_fnc_ptr $1 = (const init_fnc_t) 0x55555559c114 (gdb) Signed-off-by: Simon Glass --- arch/sandbox/cpu/os.c | 37 ++++++++++++++++++++++++++ arch/sandbox/cpu/start.c | 1 + arch/sandbox/include/asm/global_data.h | 1 + common/board_f.c | 2 ++ include/os.h | 11 ++++++++ 5 files changed, 52 insertions(+) diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index 325ded51d8a..8f6c5dbb2cd 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -706,3 +706,40 @@ int os_mprotect_allow(void *start, size_t len) return mprotect(start, len, PROT_READ | PROT_WRITE); } + +void *os_find_text_base(struct sandbox_state *state) +{ + char line[500]; + void *base = NULL; + int len; + int fd; + + /* + * This code assumes that the first line of /proc/self/maps holds + * information about the text, for example: + * + * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot + * + * The first hex value is assumed to be the address. + * + * This is tested in Linux 4.15. + */ + fd = open("/proc/self/maps", O_RDONLY); + if (fd == -1) + return NULL; + len = read(fd, line, sizeof(line)); + if (len > 0) { + char *end = memchr(line, '-', len); + + if (end) { + unsigned long long addr; + + *end = '\0'; + if (sscanf(line, "%llx", &addr) == 1) + base = (void *)addr; + } + } + close(fd); + + return base; +} diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index 2ee3b485657..17c95f89352 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -323,6 +323,7 @@ int main(int argc, char *argv[]) gd->default_log_level = state->default_log_level; #endif setup_ram_buf(state); + gd->arch.text_base = os_find_text_base(state); /* Do pre- and post-relocation init */ board_init_f(0); diff --git a/arch/sandbox/include/asm/global_data.h b/arch/sandbox/include/asm/global_data.h index f6a6a343d25..f4ce72d5660 100644 --- a/arch/sandbox/include/asm/global_data.h +++ b/arch/sandbox/include/asm/global_data.h @@ -12,6 +12,7 @@ /* Architecture-specific global data */ struct arch_global_data { uint8_t *ram_buf; /* emulated RAM buffer */ + void *text_base; /* pointer to base of text region */ }; #include diff --git a/common/board_f.c b/common/board_f.c index f1a1432d869..4600a73db1b 100644 --- a/common/board_f.c +++ b/common/board_f.c @@ -682,6 +682,8 @@ static int setup_reloc(void) * just after the default vector table location, so at 0x400 */ gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400); +#elif defined(CONFIG_SANDBOX) + gd->reloc_off = gd->arch.text_base; #else gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE; #endif diff --git a/include/os.h b/include/os.h index 28eb6252849..9224c743d1a 100644 --- a/include/os.h +++ b/include/os.h @@ -350,4 +350,15 @@ int os_mprotect_allow(void *start, size_t len); */ int os_write_file(const char *name, const void *buf, int size); +/** + * os_find_text_base() - Find the text section in this running process + * + * This tries to find the address of the text section in this running process. + * It can be useful to map the address of functions to the address listed in + * the u-boot.map file. + * + * @return address if found, else NULL + */ +void *os_find_text_base(struct sandbox_state *state); + #endif