From patchwork Tue May 27 13:10:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schmidt X-Patchwork-Id: 352933 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 10F2614009A for ; Tue, 27 May 2014 23:10:29 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:references :in-reply-to:content-type; q=dns; s=default; b=W63JKXHHgqKSjBdio KbMxz5qI1E0lTHLIYg8WGm7/C8vXSDKDb+yQKGoNdhmVlom4X7wyls/afxJK+MlM ftFoWkbrsHnI6wFqd+9Y9MF9lqPpGydM8Q6oo+y/STqAfW84bKgFKU0/iQzRtpFA VJHB/IdCqnUEdzdk1FUFDUppzI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:references :in-reply-to:content-type; s=default; bh=JjwnwVcQgBucdaKrYRp5NX8 Al+g=; b=spuX6aIAY0aZ+bDq7x+M5KHraJPxGNn0nFxtN9896lwhetqQh9iZGWj 7nbvBGMmZpoxJG7TrqJHjD76sNAAtA89qsBL6pmH7nuR4lFRlwUNr/HON5Ivomzw HFvvMYa4cPAZGwkJg3D9YojNASp8wf2UclWUsYSdzYiUWYVhusfI= Received: (qmail 19285 invoked by alias); 27 May 2014 13:10:20 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 19273 invoked by uid 89); 27 May 2014 13:10:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=AWL, BAYES_00, UNSUBSCRIBE_BODY autolearn=no version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 27 May 2014 13:10:17 +0000 Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1WpH8u-0001Zb-6D from Bernd_Schmidt@mentor.com for gcc-patches@gcc.gnu.org; Tue, 27 May 2014 06:10:12 -0700 Received: from SVR-IES-FEM-03.mgc.mentorg.com ([137.202.0.108]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Tue, 27 May 2014 06:10:12 -0700 Received: from [127.0.0.1] (137.202.0.76) by SVR-IES-FEM-03.mgc.mentorg.com (137.202.0.108) with Microsoft SMTP Server id 14.2.247.3; Tue, 27 May 2014 14:10:09 +0100 Message-ID: <53848EAE.1010207@codesourcery.com> Date: Tue, 27 May 2014 15:10:06 +0200 From: Bernd Schmidt User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: "Joseph S. Myers" CC: GCC Patches Subject: Re: Create a library for tools like collect2 and lto-wrapper (2/2) References: <537DE6E7.7090908@codesourcery.com> <537DE766.5040505@codesourcery.com> In-Reply-To: On 05/22/2014 05:56 PM, Joseph S. Myers wrote: > On Thu, 22 May 2014, Bernd Schmidt wrote: > >> The implementations of some functions like fork_execute are changed to those >> from collect2 and the calls in lto-wrapper adapted accordingly. There are some >> minor changes in these functions: for example I avoid calling fatal_error, >> instead using the fatal/fatal_perror functions so that the cleanup routine is >> called properly (lto-wrapper does not use atexit as collect2 does). > > Avoiding fatal_error seems like the wrong direction. Using the common > diagnostics code from diagnostic.c is preferred to using custom code. Hmm, honestly I would have gone the other way as diagnostic.c seems rather unnecessarily heavyweight for the purposes of these little utilities. > Thus, as an initial step I'd suggest instead converting lto-wrapper to use > the common functionality where possible (fatal_error with %m replacing > fatal_perror), making it use atexit in the process. Don't move code using > legacy fatal / fatal_perror functions to a common library. But whatever. Here is such a patch. Bootstrapped and tested on x86_64-linux. Ok? Bernd * lto-wrapper.c (fatal, fatal_perror): Remove functions. All callers changed to use fatal_error. (main): Ensure lto_wrapper_cleanup is run atexit. diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c index f59d74e..f42b14e 100644 --- a/gcc/lto-wrapper.c +++ b/gcc/lto-wrapper.c @@ -111,43 +111,6 @@ fatal_signal (int signum) kill (getpid (), signum); } -/* Just die. CMSGID is the error message. */ - -static void __attribute__ ((format (printf, 1, 2))) -fatal (const char * cmsgid, ...) -{ - va_list ap; - - va_start (ap, cmsgid); - fprintf (stderr, "lto-wrapper: "); - vfprintf (stderr, _(cmsgid), ap); - fprintf (stderr, "\n"); - va_end (ap); - - lto_wrapper_cleanup (); - exit (FATAL_EXIT_CODE); -} - - -/* Die when sys call fails. CMSGID is the error message. */ - -static void __attribute__ ((format (printf, 1, 2))) -fatal_perror (const char *cmsgid, ...) -{ - int e = errno; - va_list ap; - - va_start (ap, cmsgid); - fprintf (stderr, "lto-wrapper: "); - vfprintf (stderr, _(cmsgid), ap); - fprintf (stderr, ": %s\n", xstrerror (e)); - va_end (ap); - - lto_wrapper_cleanup (); - exit (FATAL_EXIT_CODE); -} - - /* Execute a program, and wait for the reply. ARGV are the arguments. The last one must be NULL. */ @@ -174,7 +137,7 @@ collect_execute (char **argv) pex = pex_init (0, "lto-wrapper", NULL); if (pex == NULL) - fatal_perror ("pex_init failed"); + fatal_error ("pex_init failed: %m"); /* Do not use PEX_LAST here, we use our stdout for communicating with collect2 or the linker-plugin. Any output from the sub-process @@ -186,10 +149,10 @@ collect_execute (char **argv) if (err != 0) { errno = err; - fatal_perror (errmsg); + fatal_error ("%s: %m", _(errmsg)); } else - fatal (errmsg); + fatal_error (errmsg); } return pex; @@ -205,7 +168,7 @@ collect_wait (const char *prog, struct pex_obj *pex) int status; if (!pex_get_status (pex, 1, &status)) - fatal_perror ("can't get program status"); + fatal_error ("can't get program status: %m"); pex_free (pex); if (status) @@ -214,15 +177,15 @@ collect_wait (const char *prog, struct pex_obj *pex) { int sig = WTERMSIG (status); if (WCOREDUMP (status)) - fatal ("%s terminated with signal %d [%s], core dumped", + fatal_error ("%s terminated with signal %d [%s], core dumped", prog, sig, strsignal (sig)); else - fatal ("%s terminated with signal %d [%s]", + fatal_error ("%s terminated with signal %d [%s]", prog, sig, strsignal (sig)); } if (WIFEXITED (status)) - fatal ("%s returned %d exit status", prog, WEXITSTATUS (status)); + fatal_error ("%s returned %d exit status", prog, WEXITSTATUS (status)); } return 0; @@ -238,7 +201,7 @@ maybe_unlink_file (const char *file) { if (unlink_if_ordinary (file) && errno != ENOENT) - fatal_perror ("deleting LTRANS file %s", file); + fatal_error ("deleting LTRANS file %s: %m", file); } else if (verbose) fprintf (stderr, "[Leaving LTRANS %s]\n", file); @@ -260,12 +223,12 @@ fork_execute (char **argv) at_args = concat ("@", args_name, NULL); args = fopen (args_name, "w"); if (args == NULL) - fatal ("failed to open %s", args_name); + fatal_error ("failed to open %s", args_name); status = writeargv (&argv[1], args); if (status) - fatal ("could not write to temporary file %s", args_name); + fatal_error ("could not write to temporary file %s", args_name); fclose (args); @@ -312,7 +275,7 @@ get_options_from_collect_gcc_options (const char *collect_gcc, do { if (argv_storage[j] == '\0') - fatal ("malformed COLLECT_GCC_OPTIONS"); + fatal_error ("malformed COLLECT_GCC_OPTIONS"); else if (strncmp (&argv_storage[j], "'\\''", 4) == 0) { argv_storage[k++] = '\''; @@ -452,8 +415,8 @@ merge_and_complain (struct cl_decoded_option **decoded_options, if ((*decoded_options)[j].opt_index == foption->opt_index) break; if (j == *decoded_options_count) - fatal ("Option %s not used consistently in all LTO input files", - foption->orig_option_with_args_text); + fatal_error ("Option %s not used consistently in all LTO input" + " files", foption->orig_option_with_args_text); break; case OPT_O: @@ -554,10 +517,10 @@ run_gcc (unsigned argc, char *argv[]) /* Get the driver and options. */ collect_gcc = getenv ("COLLECT_GCC"); if (!collect_gcc) - fatal ("environment variable COLLECT_GCC must be set"); + fatal_error ("environment variable COLLECT_GCC must be set"); collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS"); if (!collect_gcc_options) - fatal ("environment variable COLLECT_GCC_OPTIONS must be set"); + fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set"); get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options, CL_LANG_ALL, &decoded_options, @@ -871,7 +834,7 @@ run_gcc (unsigned argc, char *argv[]) struct obstack env_obstack; if (!stream) - fatal_perror ("fopen: %s", ltrans_output_file); + fatal_error ("fopen: %s: %m", ltrans_output_file); /* Parse the list of LTRANS inputs from the WPA stage. */ obstack_init (&env_obstack); @@ -1040,6 +1003,9 @@ main (int argc, char *argv[]) xmalloc_set_program_name (progname); + if (atexit (lto_wrapper_cleanup) != 0) + fatal_error ("atexit failed"); + gcc_init_libintl (); diagnostic_initialize (global_dc, 0);