From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id BD20C3858D38 for ; Mon, 23 Jan 2023 23:01:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BD20C3858D38 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=golang.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=google.com Received: by mail-ej1-x62c.google.com with SMTP id az20so34645848ejc.1 for ; Mon, 23 Jan 2023 15:01:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=golang-org.20210112.gappssmtp.com; s=20210112; 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=GkmSi490JaRW5CJ8ueRpibMp8Sb9cc6CH9+uLw8RpSI=; b=NeYKDoJvjoIvGdCIj73KGRgjda6sLiWcXWy465hshjmxWAx1Y598639um5QlGgBAkr +0gsdzf58utqAsP4tnXffgUM+rM2aZruj8OcAWWK1OPURTMp8QCqWC6W+xYHzSSPB8k5 bFrcZRZJgEe61IWsSWcku0cTt28Ir+BWk2TyFL3GN7HTzkcKtp0Jiqaq32wKKT6TOuMW +T9I2vv373ywTGyStrmY1CanWMKdk5XkN0LZ1GOjcr50533CFnr2BoLaj5/qqgTkCccF /ZAdFuSrS7wxCOyqpBNxm4M/VsgdG+1vvYYHKAlNVTlDyxY/c3c1bdp4/NRATWUOKQ72 TD2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=GkmSi490JaRW5CJ8ueRpibMp8Sb9cc6CH9+uLw8RpSI=; b=bCDBO+341U4BQV+/L46HQ18PnOUmSUhO+UNpzN/ytxooEKRjOog0sHBx5BISkXrjt5 vAZzjzy8MzsQbM8GqBbKR/3iYo/9sIeQaPYhkeBieCLrXvQyzwyIftzEwfBMXr9ouB5H /4hI8NXkBrOj0fdxnFwGev6CVGZfCTZ1JE+U//QHZN3WXypnSyR96Mhln2rPdyHs2RpJ ExIe0rkxxqhpbEWQiHV4i0KiE2fcj8hRjuUkSAihYSH247u/9x0UMaL80xRKTp++GrGi qOGhQiKeBzGM7crf5Si9CiZBCNS4Rew2djLswdnbgfe+heZno+izH9qiWZtxzx2aJeSP hMeQ== X-Gm-Message-State: AFqh2kq03LrBrcgYe4nmVflXifMc8vYeYb5T/j3EEBA3NtlofBdrwepS 6/ZTfatmQdQzFOHQ9j2m8urerXLxJ5goemS7b2l8GVIrmn231rvv04Y= X-Google-Smtp-Source: AMrXdXswQ3shJ6vynAvO4khRPE1bWRpAPApWsVKTmTDcZXS/xyMTgfPgsrEe11qKjTIj9kJToyz3CDo0Ay19xZzxPj8= X-Received: by 2002:a17:907:d302:b0:873:25b6:15b5 with SMTP id vg2-20020a170907d30200b0087325b615b5mr3125499ejc.284.1674514868153; Mon, 23 Jan 2023 15:01:08 -0800 (PST) MIME-Version: 1.0 References: <20230120105409.54949-1-gcc@hazardy.de> <20230120105409.54949-2-gcc@hazardy.de> In-Reply-To: <20230120105409.54949-2-gcc@hazardy.de> From: Ian Lance Taylor Date: Mon, 23 Jan 2023 15:00:56 -0800 Message-ID: Subject: Re: [PATCH 2/4] libbacktrace: detect executable path on windows 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=-11.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,USER_IN_DEF_SPF_WL autolearn=no 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:56 AM Bj=C3=B6rn Sch=C3=A4pers w= rote: > > From: Bj=C3=B6rn Sch=C3=A4pers > > This is actually needed so that libstdc++'s implementation > to be able to work on windows. > > Tested on x86_64-linux and i686-w64-mingw32. > > -- >8 -- > > * configure.ac: Add a check for windows.h. > * configure, config.h.in: Regenerate. > * fileline.c: Add windows_get_executable_path. > * fileline.c (fileline_initialiez): Add a pass using > windows_get_executable_path. > > +#ifdef HAVE_WINDOWS_H > + > +static char * > +windows_get_executable_path (char *buf, backtrace_error_callback error_c= allback, > + void *data) > +{ > + if (GetModuleFileNameA (NULL, buf, MAX_PATH - 1) =3D=3D 0) > + { > + error_callback (data, > + "could not get the filename of the current executab= le", > + (int) GetLastError ()); > + return NULL; > + } > + return buf; > +} Thanks, but this seems incomplete. The docs for GetModuleFileNameA say that if the pathname is too long to fit into the buffer it returns the size of the buffer and sets the error to ERROR_INSUFFICIENT_BUFFER. It seems to me that in that case we should allocate a larger buffer and try again. And, in general, it will be simpler if we always allocate the buffer, as macho_get_executable_path does. Unfortunately it appears that Windows does not provide a way to ask for the required length. On Windows it seems that MAX_PATH is not a true limit, as an extended length path may be up to 32767 bytes. So probably something like (untested) static char * windows_get_executable_path (struct backtrace_state *state, backtrace_error_callback error_callback, void *data) { uint32_t len; char *buf; len =3D MAX_PATH; while (1) { uint32_t got; name =3D (char *) backtrace_alloc (state, len, error_callback, data); if (name =3D=3D NULL) return NULL; got =3D GetModuleFileNameA (NULL, name, len); if (got < len - 1) /* -1 because NULB is not counted */ return name; backtrace_free (state, name, len, error_callback, data); if (GetLastError () !=3D ERROR_INSUFFICIENT_BUFFER) return NULL; len *=3D 2; } } Ian