From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id BAA763847705 for ; Thu, 30 Nov 2023 19:54:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BAA763847705 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=google.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BAA763847705 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::635 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701374048; cv=none; b=mfAapPF55U8i89GmIx7oOXS96LbxM1gx5hnzGs6Ll3fBvRpTei67WR5e8wOVJvcF0+pk0rMI9qhxGXpuz1tCB/n+HjnqIWZq0dbDU8lglGgsT6yFCnegEVCaM8+va4JSB2WV7zOYmoPsocF/X5HFQc3pjq7wg/JibzteFYZgB4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701374048; c=relaxed/simple; bh=RgaPnVeoMBDu8N3qdUS2dHWfdowmTybTfJrw4sucyXs=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=UiMgI8iycCrn88kA/6vfEmEy2DovN6oRVsrhSImG6jz3JpjlAO8aWPeTgSRP8NHchyYloE5RuBzfs0m34qyF2SPAOmibyYv9sSU3PK9enPSIY/9Goy8GMnFHlYZK1G6tKYEsR/jgh2qn7/67W0UrGvs1fKnYAX8VLPzS6Xiqua4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1d03fb57b69so7006215ad.1 for ; Thu, 30 Nov 2023 11:54:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701374046; x=1701978846; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=k7qq+st20TdMJlT9Kv61NxHCJAGCafqa/s7lqGlQtfk=; b=sOxOGZF94YAylLnQX9XLlstf9iqnJf5zDveYcLNxYeh0FEeqVNz6kVb1cYT//UDzsU OFXQ6fOw2w1sWzQDBMBpcevJwEixRWA1ZO+ezVORz8Z/MWetbVyz4XqRJA5tK6XyZid1 hn2hxaX5IkrO+dBpDOH9iAKOO1GyuC3mm2YZfdn6ENJ7YnwukBCT0P3ymqF99ZY3WQYJ xSeXyqLKaST77Rv9biZFyu7J2GO1oPH7gcViAXQ1U7orOQePtkjtZOdAk3mLt6qyApcW K/bzVesTmr2SJ44SS6ild6QXzFXQOVuMa62Faq4E/3pRgepLm2hZnrHg5W5fSnFOXvmk /S6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701374046; x=1701978846; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=k7qq+st20TdMJlT9Kv61NxHCJAGCafqa/s7lqGlQtfk=; b=VzTFGeo5zLaBxT9t3q99hfXocUMt625jG103SNB502CTCj9vRGG+dfIpjB8mJlTEH9 gUe30YQVJeHxIgzkw9geblYncxyjoGisr9GHXr77cTycV1ddIgX9DPo00aO7sFb7B+b2 JuZc6LtUL5Ea6aYnD4DeHfYFhVhWxI+vJT13RUaReWPZLDNFUF9AgVwensfoh4paVrud ytX39xN81vaOgJSVaS529fFqjFdY5qlosTbIw7B4X2ngPmV5rCXdbf06csX7FOjL1YSG 7yudgyWIzecmgiMVgcHdzk5wTSfJ7COFJJZvuCjlMKw2A+vmZtqK/UlYb/dj3igGRSQ8 O5oQ== X-Gm-Message-State: AOJu0YwPJ+m9tDY3HZhXVEIJfItLFJXOMpjEisu7U7s6mLr/zQpbtKey GGBE4O620RxQX8HkCuuZyiQNEYHTfQBZeLnUFShtrQ== X-Google-Smtp-Source: AGHT+IHPiAPJPgM2l5TOujUZ4XMaGNnOWKvJNCCmDq+3mh0oOJ1d+1bAbvG056QlON6dwOZsxGcUhAvteqS+GW63v58= X-Received: by 2002:a17:90b:180a:b0:280:a464:e9d4 with SMTP id lw10-20020a17090b180a00b00280a464e9d4mr21366101pjb.8.1701374045481; Thu, 30 Nov 2023 11:54:05 -0800 (PST) MIME-Version: 1.0 References: <20230120105409.54949-1-gcc@hazardy.de> <20230120105409.54949-4-gcc@hazardy.de> In-Reply-To: <20230120105409.54949-4-gcc@hazardy.de> From: Ian Lance Taylor Date: Thu, 30 Nov 2023 11:53:54 -0800 Message-ID: Subject: Re: [PATCH 4/4] libbacktrace: get debug information for loaded dlls To: =?UTF-8?B?QmrDtnJuIFNjaMOkcGVycw==?= Cc: gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-27.3 required=5.0 tests=BAYES_00,DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Jan 20, 2023 at 2:55=E2=80=AFAM Bj=C3=B6rn Sch=C3=A4pers wrote: > > From: Bj=C3=B6rn Sch=C3=A4pers > > Fixes https://github.com/ianlancetaylor/libbacktrace/issues/53, except > that libraries loaded after the backtrace_initialize are not handled. > But as far as I can see that's the same for elf. Thanks, but I don't want a patch that loops using goto statements. Please rewrite to avoid that. It may be simpler to call a function. Also starting with a module count of 1000 seems like a lot. Do typical Windows programs load that many modules? Ian > Tested on x86_64-linux and i686-w64-mingw32. > > -- >8 -- > > * pecoff.c (coff_add): New argument for the module handle of the > file, to get the base address. > * pecoff.c (backtrace_initialize): Iterate over loaded libraries > and call coff_add. > > Signed-off-by: Bj=C3=B6rn Sch=C3=A4pers > --- > libbacktrace/pecoff.c | 76 ++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 72 insertions(+), 4 deletions(-) > > diff --git a/libbacktrace/pecoff.c b/libbacktrace/pecoff.c > index 296f1357b5f..40395109e51 100644 > --- a/libbacktrace/pecoff.c > +++ b/libbacktrace/pecoff.c > @@ -49,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. */ > #endif > > #include > +#include > #endif > > /* Coff file header. */ > @@ -592,7 +593,8 @@ coff_syminfo (struct backtrace_state *state, uintptr_= t addr, > static int > coff_add (struct backtrace_state *state, int descriptor, > backtrace_error_callback error_callback, void *data, > - fileline *fileline_fn, int *found_sym, int *found_dwarf) > + fileline *fileline_fn, int *found_sym, int *found_dwarf, > + uintptr_t module_handle ATTRIBUTE_UNUSED) > { > struct backtrace_view fhdr_view; > off_t fhdr_off; > @@ -623,7 +625,6 @@ coff_add (struct backtrace_state *state, int descript= or, > int is_64; > uintptr_t image_base; > uintptr_t base_address =3D 0; > - uintptr_t module_handle; > struct dwarf_sections dwarf_sections; > > *found_sym =3D 0; > @@ -871,7 +872,6 @@ coff_add (struct backtrace_state *state, int descript= or, > } > > #ifdef HAVE_WINDOWS_H > - module_handle =3D (uintptr_t) GetModuleHandleW (NULL); > base_address =3D module_handle - image_base; > #endif > > @@ -914,12 +914,80 @@ backtrace_initialize (struct backtrace_state *state= , > int found_sym; > int found_dwarf; > fileline coff_fileline_fn; > + uintptr_t module_handle =3D 0; > + > +#ifdef HAVE_WINDOWS_H > + DWORD i; > + DWORD module_count; > + DWORD bytes_needed_for_modules; > + HMODULE *modules; > + char module_name[MAX_PATH]; > + int module_found_sym; > + fileline module_fileline_fn; > + > + module_handle =3D (uintptr_t) GetModuleHandleW (NULL); > +#endif > > ret =3D coff_add (state, descriptor, error_callback, data, > - &coff_fileline_fn, &found_sym, &found_dwarf); > + &coff_fileline_fn, &found_sym, &found_dwarf, module_han= dle); > if (!ret) > return 0; > > +#ifdef HAVE_WINDOWS_H > + module_count =3D 1000; > + alloc_modules: > + modules =3D backtrace_alloc (state, module_count * sizeof(HMODULE), > + error_callback, data); > + if (modules =3D=3D NULL) > + goto skip_modules; > + if (!EnumProcessModules (GetCurrentProcess (), modules, module_count, > + &bytes_needed_for_modules)) > + { > + error_callback(data, "Could not enumerate process modules", > + (int) GetLastError ()); > + goto free_modules; > + } > + if (bytes_needed_for_modules > module_count * sizeof(HMODULE)) > + { > + backtrace_free (state, modules, module_count * sizeof(HMODULE), > + error_callback, data); > + // Add an extra of 2, if some module is loaded in another thread. > + module_count =3D bytes_needed_for_modules / sizeof(HMODULE) + 2; > + modules =3D NULL; > + goto alloc_modules; > + } > + > + for (i =3D 0; i < bytes_needed_for_modules / sizeof(HMODULE); ++i) > + { > + if (GetModuleFileNameA (modules[i], module_name, MAX_PATH - 1)) > + { > + if (strcmp (filename, module_name) =3D=3D 0) > + continue; > + > + module_handle =3D (uintptr_t) GetModuleHandleA (module_name); > + if (module_handle =3D=3D 0) > + continue; > + > + descriptor =3D backtrace_open (module_name, error_callback, dat= a, NULL); > + if (descriptor < 0) > + continue; > + > + coff_add (state, descriptor, error_callback, data, > + &module_fileline_fn, &module_found_sym, &found_dwarf, > + module_handle); > + if (module_found_sym) > + found_sym =3D 1; > + } > + } > + > + free_modules: > + if (modules) > + backtrace_free(state, modules, module_count * sizeof(HMODULE), > + error_callback, data); > + modules =3D NULL; > + skip_modules: > +#endif > + > if (!state->threaded) > { > if (found_sym) > -- > 2.38.1 >