From nobody Mon Oct 6 06:46:46 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C7682ECD04 for ; Thu, 24 Jul 2025 16:33:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753374812; cv=none; b=eVS8tGRN31+vUkpS4/UOsjGI+9tcOKA5+5p7k2EsMKn8+BCB8HepKD8Ilyc6EYMOGq/4+tEBd//u4P8Dlo5ZP93um4zTSVtGMxc7MtkJEgfir2Hy7fVki4PXkR1TVoAo/eCCb3u+Z2bWSGza5L85SBCeDP8BzmXAX5B4xZ6A/Ww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753374812; c=relaxed/simple; bh=NB1Ep094ITZzt/6hL2X0/hohe+yyinJWgy+rT7836GI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=nbUunrL7ln3GqoSDoHF2x97aN+ueBd/vRgpRmQ7paxEIaf4WMkUjb2Q7sMuMI1JnIUJ7QAzTAFnsxkplaJi8oaYhRKelpL3nzAkY0D1vi8Ric/kqWKZTfdz6GXSGP4LisVqzNihhjn44BlUe0q06atHc/5u2Ol5g5OO7yvJfddc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0NQ8HI6v; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0NQ8HI6v" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-235c897d378so12372405ad.1 for ; Thu, 24 Jul 2025 09:33:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753374810; x=1753979610; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=WK9EPzbeJ+qRkJXWyqUsAEwwFgVrveIpmCMv0lOijV0=; b=0NQ8HI6vSEzGJ6ZzqVFjZuempPvEOfRsfph/Zvqs/T4NrpzwrUQRUP1HIzsM/CkqK0 3uu7IvxW1ng9iJA2LLR2PFJqq6KXYt3kBMulFYcV+R22FrzFfiIoJx8jbVRq8L6HaUIV jkHg4NGvxKYbIPKcT1A0a0Wwp8g9M4kCnBToYr5vf8OGGsovsIAMVJSWImDwY5RyXSKj gSL1JMYFHHv57wjKHo2B3w0mpX95M7XMh6mUKEqVxy/EjzaaPVkrn3M+cSdVggqWPbQT 5RjJYPVo1l7BUvVbje0ETb8RaNRdGpqQVcwIQoGy2t6bUsEf67/lqK8brViDG6CcWr4d e5vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753374810; x=1753979610; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WK9EPzbeJ+qRkJXWyqUsAEwwFgVrveIpmCMv0lOijV0=; b=Cj/sEJG1QJ0EufuJTNLZO9CdXZUQULotCpPIWPvCFl18R2ctLadY9pilres7ARsA1U tpW0hzGARBZ4GOGKUGobPGdmUH4J1vVHryVKbRjNHf0cfhnnPPzPQ0CFxEX7pIsr2FBH hNRHMWsKIW6TuRj98ofxfO20HHet3MMtkrkFFuC6vsZkde1N0heFqDFWRmXELIopJkdv k70nC/DU+2QyyT5nxvYlJhdUz2qmau1Ckm1fD98JXVaJ8daS1jT/JfmvULFYf+3OxEfs quYceGVgZUmvsKiRqPmnZ0WS06sgsFFnmP3e6XTUkXnJpCsJbkuj1dSi43coW0cpOM9C gRPg== X-Forwarded-Encrypted: i=1; AJvYcCU/qyMiWvmUaVp8UjjGIVbHHTpxAn1l8nJrbPQgc74i3/SGmKXdauk9pj634Bv25b0+ITE7q8XOShvs/NU=@vger.kernel.org X-Gm-Message-State: AOJu0YwJcfTwOtwGPc1I5zlhylW4IkTH4XYvcQOnEbbF76/Nxr7U6EKc 86swqABKTwCJdg9PkwHln62ie5JtwN4jMVzAN0bp1pmEi6GTTMyrCE7pSY4RkwEvOk7u4BKAngk 31vrZY69Z+A== X-Google-Smtp-Source: AGHT+IFa1hINpUzyy4+2Wq3CNUWdFtyLTXMCev827tSWLYrHUUWQBWe8PbpTgJm2JcOJn8pzHz1WFqjN0ZKU X-Received: from plhw7.prod.google.com ([2002:a17:903:2f47:b0:234:1073:5b87]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:40d2:b0:234:d7c5:a0ea with SMTP id d9443c01a7336-23f981bb9abmr114443315ad.24.1753374809715; Thu, 24 Jul 2025 09:33:29 -0700 (PDT) Date: Thu, 24 Jul 2025 09:32:46 -0700 In-Reply-To: <20250724163302.596743-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250724163302.596743-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250724163302.596743-7-irogers@google.com> Subject: [PATCH v6 06/22] perf dso: Move build_id to dso_id From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , "Masami Hiramatsu (Google)" , Ravi Bangoria , Charlie Jenkins , Colin Ian King , Andi Kleen , Dmitry Vyukov , Graham Woodward , Ilkka Koskinen , Zhongqiu Han , Yicong Yang , Athira Rajeev , Kajol Jain , Li Huafei , "Steinar H. Gunderson" , Stephen Brennan , Chun-Tse Shao , Yujie Liu , "Dr. David Alan Gilbert" , Levi Yun , Howard Chu , Weilin Wang , Thomas Falcon , Matt Fleming , "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" , Zixian Cai , Steve Clevenger , Ben Gainey , Chaitanya S Prakash , Martin Liska , "=?UTF-8?q?Martin=20Li=C5=A1ka?=" , Song Liu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The dso_id previously contained the major, minor, inode and inode generation information from a mmap2 event - the inode generation would be zero when reading from /proc/pid/maps. The build_id was in the dso. With build ID mmap2 events these fields wouldn't be initialized which would largely mean the special empty case where any dso would match for equality. This isn't desirable as if a dso is replaced we want the comparison to yield a difference. To support detecting the difference between DSOs based on build_id, move the build_id out of the DSO and into the dso_id. The dso_id is also stored in the DSO so nothing is lost. Capture in the dso_id what parts have been initialized and rename dso_id__inject to dso_id__improve_id so that it is clear the dso_id is being improved upon with additional information. With the build_id in the dso_id, use memcmp to compare for equality. Signed-off-by: Ian Rogers --- tools/perf/builtin-buildid-list.c | 2 +- tools/perf/builtin-inject.c | 36 ++++---- tools/perf/builtin-report.c | 11 ++- tools/perf/include/perf/perf_dlfilter.h | 2 +- tools/perf/tests/symbols.c | 4 +- tools/perf/util/build-id.c | 4 +- tools/perf/util/dso.c | 109 +++++++++++++----------- tools/perf/util/dso.h | 75 ++++++++-------- tools/perf/util/dsos.c | 18 ++-- tools/perf/util/machine.c | 28 +++--- tools/perf/util/map.c | 13 ++- tools/perf/util/map.h | 5 +- tools/perf/util/sort.c | 27 +++--- tools/perf/util/synthetic-events.c | 18 ++-- 14 files changed, 197 insertions(+), 155 deletions(-) diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid= -list.c index ba8ba0303920..151cd84b6dfe 100644 --- a/tools/perf/builtin-buildid-list.c +++ b/tools/perf/builtin-buildid-list.c @@ -31,7 +31,7 @@ static int buildid__map_cb(struct map *map, void *arg __m= aybe_unused) =20 memset(bid_buf, 0, sizeof(bid_buf)); if (dso__has_build_id(dso)) - build_id__snprintf(dso__bid_const(dso), bid_buf, sizeof(bid_buf)); + build_id__snprintf(dso__bid(dso), bid_buf, sizeof(bid_buf)); printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map__start(map), map__end(= map)); if (dso_long_name !=3D NULL) printf(" %s", dso_long_name); diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index b15eac0716f7..13bbb493141f 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -587,15 +587,17 @@ static int perf_event__repipe_mmap2(const struct perf= _tool *tool, struct perf_sample *sample, struct machine *machine) { - struct dso_id id; - struct dso_id *dso_id =3D NULL; + struct dso_id id =3D dso_id_empty; =20 - if (!(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) { + if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) { + build_id__init(&id.build_id, event->mmap2.build_id, event->mmap2.build_i= d_size); + } else { id.maj =3D event->mmap2.maj; id.min =3D event->mmap2.min; id.ino =3D event->mmap2.ino; id.ino_generation =3D event->mmap2.ino_generation; - dso_id =3D &id; + id.mmap2_valid =3D true; + id.mmap2_ino_generation_valid =3D true; } =20 return perf_event__repipe_common_mmap( @@ -603,7 +605,7 @@ static int perf_event__repipe_mmap2(const struct perf_t= ool *tool, event->mmap2.pid, event->mmap2.tid, event->mmap2.start, event->mmap2.len, event->mmap2.pgoff, event->mmap2.flags, event->mmap2.prot, - event->mmap2.filename, dso_id, + event->mmap2.filename, &id, perf_event__process_mmap2); } =20 @@ -671,19 +673,20 @@ static int perf_event__repipe_tracing_data(struct per= f_session *session, static int dso__read_build_id(struct dso *dso) { struct nscookie nsc; + struct build_id bid =3D { .size =3D 0, }; =20 if (dso__has_build_id(dso)) return 0; =20 mutex_lock(dso__lock(dso)); nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - if (filename__read_build_id(dso__long_name(dso), dso__bid(dso)) > 0) - dso__set_has_build_id(dso); + if (filename__read_build_id(dso__long_name(dso), &bid) > 0) + dso__set_build_id(dso, &bid); else if (dso__nsinfo(dso)) { char *new_name =3D dso__filename_with_chroot(dso, dso__long_name(dso)); =20 - if (new_name && filename__read_build_id(new_name, dso__bid(dso)) > 0) - dso__set_has_build_id(dso); + if (new_name && filename__read_build_id(new_name, &bid) > 0) + dso__set_build_id(dso, &bid); free(new_name); } nsinfo__mountns_exit(&nsc); @@ -732,23 +735,26 @@ static bool perf_inject__lookup_known_build_id(struct= perf_inject *inject, struct dso *dso) { struct str_node *pos; - int bid_len; =20 strlist__for_each_entry(pos, inject->known_build_ids) { + struct build_id bid; const char *build_id, *dso_name; + size_t bid_len; =20 build_id =3D skip_spaces(pos->s); dso_name =3D strchr(build_id, ' '); bid_len =3D dso_name - pos->s; + if (bid_len > sizeof(bid.data)) + bid_len =3D sizeof(bid.data); dso_name =3D skip_spaces(dso_name); if (strcmp(dso__long_name(dso), dso_name)) continue; - for (int ix =3D 0; 2 * ix + 1 < bid_len; ++ix) { - dso__bid(dso)->data[ix] =3D (hex(build_id[2 * ix]) << 4 | - hex(build_id[2 * ix + 1])); + for (size_t ix =3D 0; 2 * ix + 1 < bid_len; ++ix) { + bid.data[ix] =3D (hex(build_id[2 * ix]) << 4 | + hex(build_id[2 * ix + 1])); } - dso__bid(dso)->size =3D bid_len / 2; - dso__set_has_build_id(dso); + bid.size =3D bid_len / 2; + dso__set_build_id(dso, &bid); return true; } return false; diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index e662e1c3a7c6..26186717fe9b 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -861,17 +861,24 @@ static int maps__fprintf_task_cb(struct map *map, voi= d *data) struct maps__fprintf_task_args *args =3D data; const struct dso *dso =3D map__dso(map); u32 prot =3D map__prot(map); + const struct dso_id *dso_id =3D dso__id_const(dso); int ret; + char buf[SBUILD_ID_SIZE]; + + if (dso_id->mmap2_valid) + snprintf(buf, sizeof(buf), "%" PRIu64, dso_id->ino); + else + build_id__snprintf(&dso_id->build_id, buf, sizeof(buf)); =20 ret =3D fprintf(args->fp, - "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n", + "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %s %s\n", args->indent, "", map__start(map), map__end(map), prot & PROT_READ ? 'r' : '-', prot & PROT_WRITE ? 'w' : '-', prot & PROT_EXEC ? 'x' : '-', map__flags(map) ? 's' : 'p', map__pgoff(map), - dso__id_const(dso)->ino, dso__name(dso)); + buf, dso__name(dso)); =20 if (ret < 0) return ret; diff --git a/tools/perf/include/perf/perf_dlfilter.h b/tools/perf/include/p= erf/perf_dlfilter.h index 16fc4568ac53..2d3540ed3c58 100644 --- a/tools/perf/include/perf/perf_dlfilter.h +++ b/tools/perf/include/perf/perf_dlfilter.h @@ -87,7 +87,7 @@ struct perf_dlfilter_al { __u8 is_64_bit; /* Only valid if dso is not NULL */ __u8 is_kernel_ip; /* True if in kernel space */ __u32 buildid_size; - __u8 *buildid; + const __u8 *buildid; /* Below members are only populated by resolve_ip() */ __u8 filtered; /* True if this sample event will be filtered out */ const char *comm; diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c index ee20a366f32f..b07fdf831868 100644 --- a/tools/perf/tests/symbols.c +++ b/tools/perf/tests/symbols.c @@ -96,8 +96,8 @@ static int create_map(struct test_info *ti, char *filenam= e, struct map **map_p) dso__put(dso); =20 /* Create a dummy map at 0x100000 */ - *map_p =3D map__new(ti->machine, 0x100000, 0xffffffff, 0, NULL, - PROT_EXEC, 0, NULL, filename, ti->thread); + *map_p =3D map__new(ti->machine, 0x100000, 0xffffffff, 0, &dso_id_empty, + PROT_EXEC, /*flags=3D*/0, filename, ti->thread); if (!*map_p) { pr_debug("Failed to create map!"); return TEST_FAIL; diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index 1abd5a670665..e2b295fe4d2f 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -251,7 +251,7 @@ char *__dso__build_id_filename(const struct dso *dso, c= har *bf, size_t size, if (!dso__has_build_id(dso)) return NULL; =20 - build_id__snprintf(dso__bid_const(dso), sbuild_id, sizeof(sbuild_id)); + build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); linkname =3D build_id_cache__linkname(sbuild_id, NULL, 0); if (!linkname) return NULL; @@ -334,7 +334,7 @@ static int machine__write_buildid_table_cb(struct dso *= dso, void *data) } =20 in_kernel =3D dso__kernel(dso) || is_kernel_module(name, PERF_RECORD_MISC= _CPUMODE_UNKNOWN); - return write_buildid(name, name_len, dso__bid(dso), args->machine->pid, + return write_buildid(name, name_len, &dso__id(dso)->build_id, args->machi= ne->pid, in_kernel ? args->kmisc : args->umisc, args->fd); } =20 diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 4ff94029632e..282e3af85d5a 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -217,7 +217,7 @@ int dso__read_binary_type_filename(const struct dso *ds= o, break; } =20 - build_id__snprintf(dso__bid_const(dso), build_id_hex, sizeof(build_id_he= x)); + build_id__snprintf(dso__bid(dso), build_id_hex, sizeof(build_id_hex)); len =3D __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/"= ); snprintf(filename + len, size - len, "%.2s/%s.debug", build_id_hex, build_id_hex + 2); @@ -1382,64 +1382,76 @@ static void dso__set_long_name_id(struct dso *dso, = const char *name, bool name_a =20 static int __dso_id__cmp(const struct dso_id *a, const struct dso_id *b) { - if (a->maj > b->maj) return -1; - if (a->maj < b->maj) return 1; + if (a->mmap2_valid && b->mmap2_valid) { + if (a->maj > b->maj) return -1; + if (a->maj < b->maj) return 1; =20 - if (a->min > b->min) return -1; - if (a->min < b->min) return 1; + if (a->min > b->min) return -1; + if (a->min < b->min) return 1; =20 - if (a->ino > b->ino) return -1; - if (a->ino < b->ino) return 1; - - /* - * Synthesized MMAP events have zero ino_generation, avoid comparing - * them with MMAP events with actual ino_generation. - * - * I found it harmful because the mismatch resulted in a new - * dso that did not have a build ID whereas the original dso did have a - * build ID. The build ID was essential because the object was not found - * otherwise. - Adrian - */ - if (a->ino_generation && b->ino_generation) { + if (a->ino > b->ino) return -1; + if (a->ino < b->ino) return 1; + } + if (a->mmap2_ino_generation_valid && b->mmap2_ino_generation_valid) { if (a->ino_generation > b->ino_generation) return -1; if (a->ino_generation < b->ino_generation) return 1; } - + if (build_id__is_defined(&a->build_id) && build_id__is_defined(&b->build_= id)) { + if (a->build_id.size !=3D b->build_id.size) + return a->build_id.size < b->build_id.size ? -1 : 1; + return memcmp(a->build_id.data, b->build_id.data, a->build_id.size); + } return 0; } =20 -bool dso_id__empty(const struct dso_id *id) -{ - if (!id) - return true; - - return !id->maj && !id->min && !id->ino && !id->ino_generation; -} +const struct dso_id dso_id_empty =3D { + { + .maj =3D 0, + .min =3D 0, + .ino =3D 0, + .ino_generation =3D 0, + }, + .mmap2_valid =3D false, + .mmap2_ino_generation_valid =3D false, + { + .size =3D 0, + } +}; =20 -void __dso__inject_id(struct dso *dso, const struct dso_id *id) +void __dso__improve_id(struct dso *dso, const struct dso_id *id) { struct dsos *dsos =3D dso__dsos(dso); struct dso_id *dso_id =3D dso__id(dso); + bool changed =3D false; =20 /* dsos write lock held by caller. */ =20 - dso_id->maj =3D id->maj; - dso_id->min =3D id->min; - dso_id->ino =3D id->ino; - dso_id->ino_generation =3D id->ino_generation; - - if (dsos) + if (id->mmap2_valid && !dso_id->mmap2_valid) { + dso_id->maj =3D id->maj; + dso_id->min =3D id->min; + dso_id->ino =3D id->ino; + dso_id->mmap2_valid =3D true; + changed =3D true; + } + if (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid= ) { + dso_id->ino_generation =3D id->ino_generation; + dso_id->mmap2_ino_generation_valid =3D true; + changed =3D true; + } + if (build_id__is_defined(&id->build_id) && !build_id__is_defined(&dso_id-= >build_id)) { + dso_id->build_id =3D id->build_id; + changed =3D true; + } + if (changed && dsos) dsos->sorted =3D false; } =20 int dso_id__cmp(const struct dso_id *a, const struct dso_id *b) { - /* - * The second is always dso->id, so zeroes if not set, assume passing - * NULL for a means a zeroed id - */ - if (dso_id__empty(a) || dso_id__empty(b)) + if (a =3D=3D &dso_id_empty || b =3D=3D &dso_id_empty) { + /* There is no valid data to compare so the comparison always returns id= entical. */ return 0; + } =20 return __dso_id__cmp(a, b); } @@ -1540,7 +1552,6 @@ struct dso *dso__new_id(const char *name, const struc= t dso_id *id) dso->loaded =3D 0; dso->rel =3D 0; dso->sorted_by_name =3D 0; - dso->has_build_id =3D 0; dso->has_srcline =3D 1; dso->a2l_fails =3D 1; dso->kernel =3D DSO_SPACE__USER; @@ -1649,15 +1660,14 @@ int dso__swap_init(struct dso *dso, unsigned char e= idata) return 0; } =20 -void dso__set_build_id(struct dso *dso, struct build_id *bid) +void dso__set_build_id(struct dso *dso, const struct build_id *bid) { - RC_CHK_ACCESS(dso)->bid =3D *bid; - RC_CHK_ACCESS(dso)->has_build_id =3D 1; + dso__id(dso)->build_id =3D *bid; } =20 -bool dso__build_id_equal(const struct dso *dso, struct build_id *bid) +bool dso__build_id_equal(const struct dso *dso, const struct build_id *bid) { - const struct build_id *dso_bid =3D dso__bid_const(dso); + const struct build_id *dso_bid =3D dso__bid(dso); =20 if (dso_bid->size > bid->size && dso_bid->size =3D=3D BUILD_ID_SIZE) { /* @@ -1676,18 +1686,20 @@ bool dso__build_id_equal(const struct dso *dso, str= uct build_id *bid) void dso__read_running_kernel_build_id(struct dso *dso, struct machine *ma= chine) { char path[PATH_MAX]; + struct build_id bid =3D { .size =3D 0, }; =20 if (machine__is_default_guest(machine)) return; sprintf(path, "%s/sys/kernel/notes", machine->root_dir); - if (sysfs__read_build_id(path, dso__bid(dso)) =3D=3D 0) - dso__set_has_build_id(dso); + sysfs__read_build_id(path, &bid); + dso__set_build_id(dso, &bid); } =20 int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir) { char filename[PATH_MAX]; + struct build_id bid =3D { .size =3D 0, }; /* * kernel module short names are of the form "[module]" and * we need just "module" here. @@ -1698,9 +1710,8 @@ int dso__kernel_module_get_build_id(struct dso *dso, "%s/sys/module/%.*s/notes/.note.gnu.build-id", root_dir, (int)strlen(name) - 1, name); =20 - if (sysfs__read_build_id(filename, dso__bid(dso)) =3D=3D 0) - dso__set_has_build_id(dso); - + sysfs__read_build_id(filename, &bid); + dso__set_build_id(dso, &bid); return 0; } =20 diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index c87564471f9b..3457d713d3c5 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -185,14 +185,33 @@ enum dso_load_errno { #define DSO__DATA_CACHE_SIZE 4096 #define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1) =20 -/* - * Data about backing storage DSO, comes from PERF_RECORD_MMAP2 meta events +/** + * struct dso_id + * + * Data about backing storage DSO, comes from PERF_RECORD_MMAP2 meta event= s, + * reading from /proc/pid/maps or synthesis of build_ids from DSOs. Possib= ly + * incomplete at any particular use. */ struct dso_id { - u32 maj; - u32 min; - u64 ino; - u64 ino_generation; + /* Data related to the mmap2 event or read from /proc/pid/maps. */ + struct { + u32 maj; + u32 min; + u64 ino; + u64 ino_generation; + }; + /** @mmap2_valid: Are the maj, min and ino fields valid? */ + bool mmap2_valid; + /** + * @mmap2_ino_generation_valid: Is the ino_generation valid? Generally + * false for /proc/pid/maps mmap event. + */ + bool mmap2_ino_generation_valid; + /** + * @build_id: A possibly populated build_id. build_id__is_defined checks + * whether it is populated. + */ + struct build_id build_id; }; =20 struct dso_cache { @@ -243,7 +262,6 @@ DECLARE_RC_STRUCT(dso) { u64 addr; struct symbol *symbol; } last_find_result; - struct build_id bid; u64 text_offset; u64 text_end; const char *short_name; @@ -276,7 +294,6 @@ DECLARE_RC_STRUCT(dso) { enum dso_swap_type needs_swap:2; bool is_kmod:1; u8 adjust_symbols:1; - u8 has_build_id:1; u8 header_build_id:1; u8 has_srcline:1; u8 hit:1; @@ -292,6 +309,9 @@ DECLARE_RC_STRUCT(dso) { }; =20 extern struct mutex _dso__data_open_lock; +extern const struct dso_id dso_id_empty; + +int dso_id__cmp(const struct dso_id *a, const struct dso_id *b); =20 /* dso__for_each_symbol - iterate over the symbols of given type * @@ -362,31 +382,11 @@ static inline void dso__set_auxtrace_cache(struct dso= *dso, struct auxtrace_cach RC_CHK_ACCESS(dso)->auxtrace_cache =3D cache; } =20 -static inline struct build_id *dso__bid(struct dso *dso) -{ - return &RC_CHK_ACCESS(dso)->bid; -} - -static inline const struct build_id *dso__bid_const(const struct dso *dso) -{ - return &RC_CHK_ACCESS(dso)->bid; -} - static inline struct dso_bpf_prog *dso__bpf_prog(struct dso *dso) { return &RC_CHK_ACCESS(dso)->bpf_prog; } =20 -static inline bool dso__has_build_id(const struct dso *dso) -{ - return RC_CHK_ACCESS(dso)->has_build_id; -} - -static inline void dso__set_has_build_id(struct dso *dso) -{ - RC_CHK_ACCESS(dso)->has_build_id =3D true; -} - static inline bool dso__has_srcline(const struct dso *dso) { return RC_CHK_ACCESS(dso)->has_srcline; @@ -462,6 +462,16 @@ static inline const struct dso_id *dso__id_const(const= struct dso *dso) return &RC_CHK_ACCESS(dso)->id; } =20 +static inline const struct build_id *dso__bid(const struct dso *dso) +{ + return &dso__id_const(dso)->build_id; +} + +static inline bool dso__has_build_id(const struct dso *dso) +{ + return build_id__is_defined(dso__bid(dso)); +} + static inline struct rb_root_cached *dso__inlined_nodes(struct dso *dso) { return &RC_CHK_ACCESS(dso)->inlined_nodes; @@ -699,9 +709,6 @@ static inline void dso__set_text_offset(struct dso *dso= , u64 val) RC_CHK_ACCESS(dso)->text_offset =3D val; } =20 -int dso_id__cmp(const struct dso_id *a, const struct dso_id *b); -bool dso_id__empty(const struct dso_id *id); - struct dso *dso__new_id(const char *name, const struct dso_id *id); struct dso *dso__new(const char *name); void dso__delete(struct dso *dso); @@ -709,7 +716,7 @@ void dso__delete(struct dso *dso); int dso__cmp_id(struct dso *a, struct dso *b); void dso__set_short_name(struct dso *dso, const char *name, bool name_allo= cated); void dso__set_long_name(struct dso *dso, const char *name, bool name_alloc= ated); -void __dso__inject_id(struct dso *dso, const struct dso_id *id); +void __dso__improve_id(struct dso *dso, const struct dso_id *id); =20 int dso__name_len(const struct dso *dso); =20 @@ -739,8 +746,8 @@ void dso__sort_by_name(struct dso *dso); =20 int dso__swap_init(struct dso *dso, unsigned char eidata); =20 -void dso__set_build_id(struct dso *dso, struct build_id *bid); -bool dso__build_id_equal(const struct dso *dso, struct build_id *bid); +void dso__set_build_id(struct dso *dso, const struct build_id *bid); +bool dso__build_id_equal(const struct dso *dso, const struct build_id *bid= ); void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine); int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir); diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index 47538273915d..0a7645c7fae7 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -72,6 +72,7 @@ static int dsos__read_build_ids_cb(struct dso *dso, void = *data) { struct dsos__read_build_ids_cb_args *args =3D data; struct nscookie nsc; + struct build_id bid =3D { .size =3D 0, }; =20 if (args->with_hits && !dso__hit(dso) && !dso__is_vdso(dso)) return 0; @@ -80,15 +81,15 @@ static int dsos__read_build_ids_cb(struct dso *dso, voi= d *data) return 0; } nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - if (filename__read_build_id(dso__long_name(dso), dso__bid(dso)) > 0) { + if (filename__read_build_id(dso__long_name(dso), &bid) > 0) { + dso__set_build_id(dso, &bid); args->have_build_id =3D true; - dso__set_has_build_id(dso); } else if (errno =3D=3D ENOENT && dso__nsinfo(dso)) { char *new_name =3D dso__filename_with_chroot(dso, dso__long_name(dso)); =20 - if (new_name && filename__read_build_id(new_name, dso__bid(dso)) > 0) { + if (new_name && filename__read_build_id(new_name, &bid) > 0) { + dso__set_build_id(dso, &bid); args->have_build_id =3D true; - dso__set_has_build_id(dso); } free(new_name); } @@ -286,7 +287,7 @@ struct dso *dsos__find(struct dsos *dsos, const char *n= ame, bool cmp_short) struct dso *res; =20 down_read(&dsos->lock); - res =3D __dsos__find_id(dsos, name, NULL, cmp_short, /*write_locked=3D*/f= alse); + res =3D __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, /*write_loc= ked=3D*/false); up_read(&dsos->lock); return res; } @@ -344,8 +345,8 @@ static struct dso *__dsos__findnew_id(struct dsos *dsos= , const char *name, const { struct dso *dso =3D __dsos__find_id(dsos, name, id, false, /*write_locked= =3D*/true); =20 - if (dso && dso_id__empty(dso__id(dso)) && !dso_id__empty(id)) - __dso__inject_id(dso, id); + if (dso) + __dso__improve_id(dso, id); =20 return dso ? dso : __dsos__addnew_id(dsos, name, id); } @@ -436,7 +437,8 @@ struct dso *dsos__findnew_module_dso(struct dsos *dsos, =20 down_write(&dsos->lock); =20 - dso =3D __dsos__find_id(dsos, m->name, NULL, /*cmp_short=3D*/true, /*writ= e_locked=3D*/true); + dso =3D __dsos__find_id(dsos, m->name, &dso_id_empty, /*cmp_short=3D*/tru= e, + /*write_locked=3D*/true); if (dso) { up_write(&dsos->lock); return dso; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 7ec12c207970..2ef8c1cfae1e 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1731,21 +1731,21 @@ int machine__process_mmap2_event(struct machine *ma= chine, { struct thread *thread; struct map *map; - struct dso_id dso_id =3D { - .maj =3D event->mmap2.maj, - .min =3D event->mmap2.min, - .ino =3D event->mmap2.ino, - .ino_generation =3D event->mmap2.ino_generation, - }; - struct build_id __bid, *bid =3D NULL; + struct dso_id dso_id =3D dso_id_empty; int ret =3D 0; =20 if (dump_trace) perf_event__fprintf_mmap2(event, stdout); =20 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) { - bid =3D &__bid; - build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size); + build_id__init(&dso_id.build_id, event->mmap2.build_id, event->mmap2.bui= ld_id_size); + } else { + dso_id.maj =3D event->mmap2.maj; + dso_id.min =3D event->mmap2.min; + dso_id.ino =3D event->mmap2.ino; + dso_id.ino_generation =3D event->mmap2.ino_generation; + dso_id.mmap2_valid =3D true; + dso_id.mmap2_ino_generation_valid =3D true; } =20 if (sample->cpumode =3D=3D PERF_RECORD_MISC_GUEST_KERNEL || @@ -1757,7 +1757,7 @@ int machine__process_mmap2_event(struct machine *mach= ine, }; =20 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN); - ret =3D machine__process_kernel_mmap_event(machine, &xm, bid); + ret =3D machine__process_kernel_mmap_event(machine, &xm, &dso_id.build_i= d); if (ret < 0) goto out_problem; return 0; @@ -1771,7 +1771,7 @@ int machine__process_mmap2_event(struct machine *mach= ine, map =3D map__new(machine, event->mmap2.start, event->mmap2.len, event->mmap2.pgoff, &dso_id, event->mmap2.prot, - event->mmap2.flags, bid, + event->mmap2.flags, event->mmap2.filename, thread); =20 if (map =3D=3D NULL) @@ -1829,8 +1829,8 @@ int machine__process_mmap_event(struct machine *machi= ne, union perf_event *event prot =3D PROT_EXEC; =20 map =3D map__new(machine, event->mmap.start, - event->mmap.len, event->mmap.pgoff, - NULL, prot, 0, NULL, event->mmap.filename, thread); + event->mmap.len, event->mmap.pgoff, + &dso_id_empty, prot, /*flags=3D*/0, event->mmap.filename, thread); =20 if (map =3D=3D NULL) goto out_problem_map; @@ -3192,7 +3192,7 @@ struct dso *machine__findnew_dso_id(struct machine *m= achine, const char *filenam =20 struct dso *machine__findnew_dso(struct machine *machine, const char *file= name) { - return machine__findnew_dso_id(machine, filename, NULL); + return machine__findnew_dso_id(machine, filename, &dso_id_empty); } =20 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *add= rp, char **modp) diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 0f6b185f9589..b46c68c24d1c 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -120,8 +120,8 @@ static void map__init(struct map *map, u64 start, u64 e= nd, u64 pgoff, } =20 struct map *map__new(struct machine *machine, u64 start, u64 len, - u64 pgoff, struct dso_id *id, - u32 prot, u32 flags, struct build_id *bid, + u64 pgoff, const struct dso_id *id, + u32 prot, u32 flags, char *filename, struct thread *thread) { struct map *result; @@ -132,7 +132,7 @@ struct map *map__new(struct machine *machine, u64 start= , u64 len, map =3D zalloc(sizeof(*map)); if (ADD_RC_CHK(result, map)) { char newfilename[PATH_MAX]; - struct dso *dso, *header_bid_dso; + struct dso *dso; int anon, no_dso, vdso, android; =20 android =3D is_android_lib(filename); @@ -189,16 +189,15 @@ struct map *map__new(struct machine *machine, u64 sta= rt, u64 len, dso__set_nsinfo(dso, nsi); mutex_unlock(dso__lock(dso)); =20 - if (build_id__is_defined(bid)) { - dso__set_build_id(dso, bid); - } else { + if (!build_id__is_defined(&id->build_id)) { /* * If the mmap event had no build ID, search for an existing dso from t= he * build ID header by name. Otherwise only the dso loaded at the time of * reading the header will have the build ID set and all future mmaps w= ill * have it missing. */ - header_bid_dso =3D dsos__find(&machine->dsos, filename, false); + struct dso *header_bid_dso =3D dsos__find(&machine->dsos, filename, fal= se); + if (header_bid_dso && dso__header_build_id(header_bid_dso)) { dso__set_build_id(dso, dso__bid(header_bid_dso)); dso__set_header_build_id(dso, 1); diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 4262f5a143be..9cadf533a561 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -173,11 +173,10 @@ struct thread; __map__for_each_symbol_by_name(map, sym_name, (pos), idx) =20 struct dso_id; -struct build_id; =20 struct map *map__new(struct machine *machine, u64 start, u64 len, - u64 pgoff, struct dso_id *id, u32 prot, u32 flags, - struct build_id *bid, char *filename, struct thread *thread); + u64 pgoff, const struct dso_id *id, u32 prot, u32 flags, + char *filename, struct thread *thread); struct map *map__new2(u64 start, struct dso *dso); void map__delete(struct map *map); struct map *map__clone(struct map *map); diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 45e654653960..7969d64a47bf 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -1746,22 +1746,27 @@ sort__dcacheline_cmp(struct hist_entry *left, struc= t hist_entry *right) if (rc) return rc; /* - * Addresses with no major/minor numbers are assumed to be + * Addresses with no major/minor numbers or build ID are assumed to be * anonymous in userspace. Sort those on pid then address. * * The kernel and non-zero major/minor mapped areas are * assumed to be unity mapped. Sort those on address. */ + if (left->cpumode !=3D PERF_RECORD_MISC_KERNEL && (map__flags(l_map) & MA= P_SHARED) =3D=3D 0) { + const struct dso_id *dso_id =3D dso__id_const(l_dso); =20 - if ((left->cpumode !=3D PERF_RECORD_MISC_KERNEL) && - (!(map__flags(l_map) & MAP_SHARED)) && !dso__id(l_dso)->maj && !dso__= id(l_dso)->min && - !dso__id(l_dso)->ino && !dso__id(l_dso)->ino_generation) { - /* userspace anonymous */ + if (!dso_id->mmap2_valid) + dso_id =3D dso__id_const(r_dso); =20 - if (thread__pid(left->thread) > thread__pid(right->thread)) - return -1; - if (thread__pid(left->thread) < thread__pid(right->thread)) - return 1; + if (!build_id__is_defined(&dso_id->build_id) && + (!dso_id->mmap2_valid || (dso_id->maj =3D=3D 0 && dso_id->min =3D=3D= 0))) { + /* userspace anonymous */ + + if (thread__pid(left->thread) > thread__pid(right->thread)) + return -1; + if (thread__pid(left->thread) < thread__pid(right->thread)) + return 1; + } } =20 addr: @@ -1786,6 +1791,7 @@ static int hist_entry__dcacheline_snprintf(struct his= t_entry *he, char *bf, if (he->mem_info) { struct map *map =3D mem_info__daddr(he->mem_info)->ms.map; struct dso *dso =3D map ? map__dso(map) : NULL; + const struct dso_id *dso_id =3D dso ? dso__id_const(dso) : &dso_id_empty; =20 addr =3D cl_address(mem_info__daddr(he->mem_info)->al_addr, chk_double_c= l); ms =3D &mem_info__daddr(he->mem_info)->ms; @@ -1794,8 +1800,7 @@ static int hist_entry__dcacheline_snprintf(struct his= t_entry *he, char *bf, if ((he->cpumode !=3D PERF_RECORD_MISC_KERNEL) && map && !(map__prot(map) & PROT_EXEC) && (map__flags(map) & MAP_SHARED) && - (dso__id(dso)->maj || dso__id(dso)->min || dso__id(dso)->ino || - dso__id(dso)->ino_generation)) + (!dso_id->mmap2_valid || (dso_id->maj =3D=3D 0 && dso_id->min =3D= =3D 0))) level =3D 's'; else if (!map) level =3D 'X'; diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index b7ebee95536f..69b98023ce74 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -372,7 +372,7 @@ static void perf_record_mmap2__read_build_id(struct per= f_record_mmap2 *event, struct nsinfo *nsi; struct nscookie nc; struct dso *dso =3D NULL; - struct dso_id id; + struct dso_id dso_id =3D dso_id_empty; int rc; =20 if (is_kernel) { @@ -380,12 +380,18 @@ static void perf_record_mmap2__read_build_id(struct p= erf_record_mmap2 *event, goto out; } =20 - id.maj =3D event->maj; - id.min =3D event->min; - id.ino =3D event->ino; - id.ino_generation =3D event->ino_generation; + if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) { + build_id__init(&dso_id.build_id, event->build_id, event->build_id_size); + } else { + dso_id.maj =3D event->maj; + dso_id.min =3D event->min; + dso_id.ino =3D event->ino; + dso_id.ino_generation =3D event->ino_generation; + dso_id.mmap2_valid =3D true; + dso_id.mmap2_ino_generation_valid =3D true; + }; =20 - dso =3D dsos__findnew_id(&machine->dsos, event->filename, &id); + dso =3D dsos__findnew_id(&machine->dsos, event->filename, &dso_id); if (dso && dso__has_build_id(dso)) { bid =3D *dso__bid(dso); rc =3D 0; --=20 2.50.0.727.gbf7dc18ff4-goog