xref: /aosp_15_r20/external/elfutils/libdwfl/argp-std.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Standard argp argument parsers for tools using libdwfl.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2005-2010, 2012, 2015 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker 
5*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker    it under the terms of either
7*7304104dSAndroid Build Coastguard Worker 
8*7304104dSAndroid Build Coastguard Worker      * the GNU Lesser General Public License as published by the Free
9*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 3 of the License, or (at
10*7304104dSAndroid Build Coastguard Worker        your option) any later version
11*7304104dSAndroid Build Coastguard Worker 
12*7304104dSAndroid Build Coastguard Worker    or
13*7304104dSAndroid Build Coastguard Worker 
14*7304104dSAndroid Build Coastguard Worker      * the GNU General Public License as published by the Free
15*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 2 of the License, or (at
16*7304104dSAndroid Build Coastguard Worker        your option) any later version
17*7304104dSAndroid Build Coastguard Worker 
18*7304104dSAndroid Build Coastguard Worker    or both in parallel, as here.
19*7304104dSAndroid Build Coastguard Worker 
20*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
21*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
22*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23*7304104dSAndroid Build Coastguard Worker    General Public License for more details.
24*7304104dSAndroid Build Coastguard Worker 
25*7304104dSAndroid Build Coastguard Worker    You should have received copies of the GNU General Public License and
26*7304104dSAndroid Build Coastguard Worker    the GNU Lesser General Public License along with this program.  If
27*7304104dSAndroid Build Coastguard Worker    not, see <http://www.gnu.org/licenses/>.  */
28*7304104dSAndroid Build Coastguard Worker 
29*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
30*7304104dSAndroid Build Coastguard Worker # include <config.h>
31*7304104dSAndroid Build Coastguard Worker #endif
32*7304104dSAndroid Build Coastguard Worker 
33*7304104dSAndroid Build Coastguard Worker #include "libdwflP.h"
34*7304104dSAndroid Build Coastguard Worker #include <argp.h>
35*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
36*7304104dSAndroid Build Coastguard Worker #include <assert.h>
37*7304104dSAndroid Build Coastguard Worker #include <fcntl.h>
38*7304104dSAndroid Build Coastguard Worker 
39*7304104dSAndroid Build Coastguard Worker 
40*7304104dSAndroid Build Coastguard Worker #define OPT_DEBUGINFO	0x100
41*7304104dSAndroid Build Coastguard Worker #define OPT_COREFILE	0x101
42*7304104dSAndroid Build Coastguard Worker 
43*7304104dSAndroid Build Coastguard Worker static const struct argp_option options[] =
44*7304104dSAndroid Build Coastguard Worker {
45*7304104dSAndroid Build Coastguard Worker   { NULL, 0, NULL, 0, N_("Input selection options:"), 0 },
46*7304104dSAndroid Build Coastguard Worker   { "executable", 'e', "FILE", 0, N_("Find addresses in FILE"), 0 },
47*7304104dSAndroid Build Coastguard Worker   { "core", OPT_COREFILE, "COREFILE", 0,
48*7304104dSAndroid Build Coastguard Worker     N_("Find addresses from signatures found in COREFILE"), 0 },
49*7304104dSAndroid Build Coastguard Worker   { "pid", 'p', "PID", 0,
50*7304104dSAndroid Build Coastguard Worker     N_("Find addresses in files mapped into process PID"), 0 },
51*7304104dSAndroid Build Coastguard Worker   { "linux-process-map", 'M', "FILE", 0,
52*7304104dSAndroid Build Coastguard Worker     N_("Find addresses in files mapped as read from FILE"
53*7304104dSAndroid Build Coastguard Worker        " in Linux /proc/PID/maps format"), 0 },
54*7304104dSAndroid Build Coastguard Worker   { "kernel", 'k', NULL, 0, N_("Find addresses in the running kernel"), 0 },
55*7304104dSAndroid Build Coastguard Worker   { "offline-kernel", 'K', "RELEASE", OPTION_ARG_OPTIONAL,
56*7304104dSAndroid Build Coastguard Worker     N_("Kernel with all modules"), 0 },
57*7304104dSAndroid Build Coastguard Worker   { "debuginfo-path", OPT_DEBUGINFO, "PATH", 0,
58*7304104dSAndroid Build Coastguard Worker     N_("Search path for separate debuginfo files"), 0 },
59*7304104dSAndroid Build Coastguard Worker   { NULL, 0, NULL, 0, NULL, 0 }
60*7304104dSAndroid Build Coastguard Worker };
61*7304104dSAndroid Build Coastguard Worker 
62*7304104dSAndroid Build Coastguard Worker static char *debuginfo_path;
63*7304104dSAndroid Build Coastguard Worker 
64*7304104dSAndroid Build Coastguard Worker static const Dwfl_Callbacks offline_callbacks =
65*7304104dSAndroid Build Coastguard Worker   {
66*7304104dSAndroid Build Coastguard Worker     .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
67*7304104dSAndroid Build Coastguard Worker     .debuginfo_path = &debuginfo_path,
68*7304104dSAndroid Build Coastguard Worker 
69*7304104dSAndroid Build Coastguard Worker     .section_address = INTUSE(dwfl_offline_section_address),
70*7304104dSAndroid Build Coastguard Worker 
71*7304104dSAndroid Build Coastguard Worker     /* We use this table for core files too.  */
72*7304104dSAndroid Build Coastguard Worker     .find_elf = INTUSE(dwfl_build_id_find_elf),
73*7304104dSAndroid Build Coastguard Worker   };
74*7304104dSAndroid Build Coastguard Worker 
75*7304104dSAndroid Build Coastguard Worker static const Dwfl_Callbacks proc_callbacks =
76*7304104dSAndroid Build Coastguard Worker   {
77*7304104dSAndroid Build Coastguard Worker     .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
78*7304104dSAndroid Build Coastguard Worker     .debuginfo_path = &debuginfo_path,
79*7304104dSAndroid Build Coastguard Worker 
80*7304104dSAndroid Build Coastguard Worker     .find_elf = INTUSE(dwfl_linux_proc_find_elf),
81*7304104dSAndroid Build Coastguard Worker   };
82*7304104dSAndroid Build Coastguard Worker 
83*7304104dSAndroid Build Coastguard Worker static const Dwfl_Callbacks kernel_callbacks =
84*7304104dSAndroid Build Coastguard Worker   {
85*7304104dSAndroid Build Coastguard Worker     .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
86*7304104dSAndroid Build Coastguard Worker     .debuginfo_path = &debuginfo_path,
87*7304104dSAndroid Build Coastguard Worker 
88*7304104dSAndroid Build Coastguard Worker     .find_elf = INTUSE(dwfl_linux_kernel_find_elf),
89*7304104dSAndroid Build Coastguard Worker     .section_address = INTUSE(dwfl_linux_kernel_module_section_address),
90*7304104dSAndroid Build Coastguard Worker   };
91*7304104dSAndroid Build Coastguard Worker 
92*7304104dSAndroid Build Coastguard Worker /* Structure held at state->HOOK.  */
93*7304104dSAndroid Build Coastguard Worker struct parse_opt
94*7304104dSAndroid Build Coastguard Worker {
95*7304104dSAndroid Build Coastguard Worker   Dwfl *dwfl;
96*7304104dSAndroid Build Coastguard Worker   /* The -e|--executable parameter.  */
97*7304104dSAndroid Build Coastguard Worker   const char *e;
98*7304104dSAndroid Build Coastguard Worker   /* The --core parameter.  */
99*7304104dSAndroid Build Coastguard Worker   const char *core;
100*7304104dSAndroid Build Coastguard Worker };
101*7304104dSAndroid Build Coastguard Worker 
102*7304104dSAndroid Build Coastguard Worker static inline void
failure(Dwfl * dwfl,int errnum,const char * msg,struct argp_state * state)103*7304104dSAndroid Build Coastguard Worker failure (Dwfl *dwfl, int errnum, const char *msg, struct argp_state *state)
104*7304104dSAndroid Build Coastguard Worker {
105*7304104dSAndroid Build Coastguard Worker   if (dwfl != NULL)
106*7304104dSAndroid Build Coastguard Worker     dwfl_end (dwfl);
107*7304104dSAndroid Build Coastguard Worker   if (errnum == -1)
108*7304104dSAndroid Build Coastguard Worker     argp_failure (state, EXIT_FAILURE, 0, "%s: %s",
109*7304104dSAndroid Build Coastguard Worker                   msg, INTUSE(dwfl_errmsg) (-1));
110*7304104dSAndroid Build Coastguard Worker   else
111*7304104dSAndroid Build Coastguard Worker     argp_failure (state, EXIT_FAILURE, errnum, "%s", msg);
112*7304104dSAndroid Build Coastguard Worker }
113*7304104dSAndroid Build Coastguard Worker 
114*7304104dSAndroid Build Coastguard Worker static inline error_t
fail(Dwfl * dwfl,int errnum,const char * msg,struct argp_state * state)115*7304104dSAndroid Build Coastguard Worker fail (Dwfl *dwfl, int errnum, const char *msg, struct argp_state *state)
116*7304104dSAndroid Build Coastguard Worker {
117*7304104dSAndroid Build Coastguard Worker   failure (dwfl, errnum, msg, state);
118*7304104dSAndroid Build Coastguard Worker   return errnum == -1 ? EIO : errnum;
119*7304104dSAndroid Build Coastguard Worker }
120*7304104dSAndroid Build Coastguard Worker 
121*7304104dSAndroid Build Coastguard Worker static error_t
parse_opt(int key,char * arg,struct argp_state * state)122*7304104dSAndroid Build Coastguard Worker parse_opt (int key, char *arg, struct argp_state *state)
123*7304104dSAndroid Build Coastguard Worker {
124*7304104dSAndroid Build Coastguard Worker   switch (key)
125*7304104dSAndroid Build Coastguard Worker     {
126*7304104dSAndroid Build Coastguard Worker     case ARGP_KEY_INIT:
127*7304104dSAndroid Build Coastguard Worker       {
128*7304104dSAndroid Build Coastguard Worker 	assert (state->hook == NULL);
129*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = calloc (1, sizeof (*opt));
130*7304104dSAndroid Build Coastguard Worker 	if (opt == NULL)
131*7304104dSAndroid Build Coastguard Worker 	  failure (NULL, DWFL_E_ERRNO, "calloc", state);
132*7304104dSAndroid Build Coastguard Worker 	state->hook = opt;
133*7304104dSAndroid Build Coastguard Worker       }
134*7304104dSAndroid Build Coastguard Worker       break;
135*7304104dSAndroid Build Coastguard Worker 
136*7304104dSAndroid Build Coastguard Worker     case OPT_DEBUGINFO:
137*7304104dSAndroid Build Coastguard Worker       debuginfo_path = arg;
138*7304104dSAndroid Build Coastguard Worker       break;
139*7304104dSAndroid Build Coastguard Worker 
140*7304104dSAndroid Build Coastguard Worker     case 'e':
141*7304104dSAndroid Build Coastguard Worker       {
142*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
143*7304104dSAndroid Build Coastguard Worker 	Dwfl *dwfl = opt->dwfl;
144*7304104dSAndroid Build Coastguard Worker 	if (dwfl == NULL)
145*7304104dSAndroid Build Coastguard Worker 	  {
146*7304104dSAndroid Build Coastguard Worker 	    dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
147*7304104dSAndroid Build Coastguard Worker 	    if (dwfl == NULL)
148*7304104dSAndroid Build Coastguard Worker 	      return fail (dwfl, -1, arg, state);
149*7304104dSAndroid Build Coastguard Worker 	    opt->dwfl = dwfl;
150*7304104dSAndroid Build Coastguard Worker 
151*7304104dSAndroid Build Coastguard Worker 	    /* Start at zero so if there is just one -e foo.so,
152*7304104dSAndroid Build Coastguard Worker 	       the DSO is shown without address bias.  */
153*7304104dSAndroid Build Coastguard Worker 	    dwfl->offline_next_address = 0;
154*7304104dSAndroid Build Coastguard Worker 	  }
155*7304104dSAndroid Build Coastguard Worker 	if (dwfl->callbacks != &offline_callbacks)
156*7304104dSAndroid Build Coastguard Worker 	  {
157*7304104dSAndroid Build Coastguard Worker 	  toomany:
158*7304104dSAndroid Build Coastguard Worker 	    argp_error (state, "%s",
159*7304104dSAndroid Build Coastguard Worker 			_("only one of -e, -p, -k, -K, or --core allowed"));
160*7304104dSAndroid Build Coastguard Worker 	    return EINVAL;
161*7304104dSAndroid Build Coastguard Worker 	  }
162*7304104dSAndroid Build Coastguard Worker 	opt->e = arg;
163*7304104dSAndroid Build Coastguard Worker       }
164*7304104dSAndroid Build Coastguard Worker       break;
165*7304104dSAndroid Build Coastguard Worker 
166*7304104dSAndroid Build Coastguard Worker     case 'p':
167*7304104dSAndroid Build Coastguard Worker       {
168*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
169*7304104dSAndroid Build Coastguard Worker 	if (opt->dwfl == NULL)
170*7304104dSAndroid Build Coastguard Worker 	  {
171*7304104dSAndroid Build Coastguard Worker 	    Dwfl *dwfl = INTUSE(dwfl_begin) (&proc_callbacks);
172*7304104dSAndroid Build Coastguard Worker 	    int result = INTUSE(dwfl_linux_proc_report) (dwfl, atoi (arg));
173*7304104dSAndroid Build Coastguard Worker 	    if (result != 0)
174*7304104dSAndroid Build Coastguard Worker 	      return fail (dwfl, result, arg, state);
175*7304104dSAndroid Build Coastguard Worker 
176*7304104dSAndroid Build Coastguard Worker 	    /* Non-fatal to not be able to attach to process, ignore error.  */
177*7304104dSAndroid Build Coastguard Worker 	    INTUSE(dwfl_linux_proc_attach) (dwfl, atoi (arg), false);
178*7304104dSAndroid Build Coastguard Worker 
179*7304104dSAndroid Build Coastguard Worker 	    opt->dwfl = dwfl;
180*7304104dSAndroid Build Coastguard Worker 	  }
181*7304104dSAndroid Build Coastguard Worker 	else
182*7304104dSAndroid Build Coastguard Worker 	  goto toomany;
183*7304104dSAndroid Build Coastguard Worker       }
184*7304104dSAndroid Build Coastguard Worker       break;
185*7304104dSAndroid Build Coastguard Worker 
186*7304104dSAndroid Build Coastguard Worker     case 'M':
187*7304104dSAndroid Build Coastguard Worker       {
188*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
189*7304104dSAndroid Build Coastguard Worker 	if (opt->dwfl == NULL)
190*7304104dSAndroid Build Coastguard Worker 	  {
191*7304104dSAndroid Build Coastguard Worker 	    FILE *f = fopen (arg, "r");
192*7304104dSAndroid Build Coastguard Worker 	    if (f == NULL)
193*7304104dSAndroid Build Coastguard Worker 	      {
194*7304104dSAndroid Build Coastguard Worker 		int code = errno;
195*7304104dSAndroid Build Coastguard Worker 		argp_failure (state, EXIT_FAILURE, code,
196*7304104dSAndroid Build Coastguard Worker 			      "cannot open '%s'", arg);
197*7304104dSAndroid Build Coastguard Worker 		return code;
198*7304104dSAndroid Build Coastguard Worker 	      }
199*7304104dSAndroid Build Coastguard Worker 	    Dwfl *dwfl = INTUSE(dwfl_begin) (&proc_callbacks);
200*7304104dSAndroid Build Coastguard Worker 	    int result = INTUSE(dwfl_linux_proc_maps_report) (dwfl, f);
201*7304104dSAndroid Build Coastguard Worker 	    fclose (f);
202*7304104dSAndroid Build Coastguard Worker 	    if (result != 0)
203*7304104dSAndroid Build Coastguard Worker 	      return fail (dwfl, result, arg, state);
204*7304104dSAndroid Build Coastguard Worker 	    opt->dwfl = dwfl;
205*7304104dSAndroid Build Coastguard Worker 	  }
206*7304104dSAndroid Build Coastguard Worker 	else
207*7304104dSAndroid Build Coastguard Worker 	  goto toomany;
208*7304104dSAndroid Build Coastguard Worker       }
209*7304104dSAndroid Build Coastguard Worker       break;
210*7304104dSAndroid Build Coastguard Worker 
211*7304104dSAndroid Build Coastguard Worker     case OPT_COREFILE:
212*7304104dSAndroid Build Coastguard Worker       {
213*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
214*7304104dSAndroid Build Coastguard Worker 	Dwfl *dwfl = opt->dwfl;
215*7304104dSAndroid Build Coastguard Worker 	if (dwfl == NULL)
216*7304104dSAndroid Build Coastguard Worker 	  opt->dwfl = dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
217*7304104dSAndroid Build Coastguard Worker 	/* Permit -e and --core together.  */
218*7304104dSAndroid Build Coastguard Worker 	else if (dwfl->callbacks != &offline_callbacks)
219*7304104dSAndroid Build Coastguard Worker 	  goto toomany;
220*7304104dSAndroid Build Coastguard Worker 	opt->core = arg;
221*7304104dSAndroid Build Coastguard Worker       }
222*7304104dSAndroid Build Coastguard Worker       break;
223*7304104dSAndroid Build Coastguard Worker 
224*7304104dSAndroid Build Coastguard Worker     case 'k':
225*7304104dSAndroid Build Coastguard Worker       {
226*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
227*7304104dSAndroid Build Coastguard Worker 	if (opt->dwfl == NULL)
228*7304104dSAndroid Build Coastguard Worker 	  {
229*7304104dSAndroid Build Coastguard Worker 	    Dwfl *dwfl = INTUSE(dwfl_begin) (&kernel_callbacks);
230*7304104dSAndroid Build Coastguard Worker 	    int result = INTUSE(dwfl_linux_kernel_report_kernel) (dwfl);
231*7304104dSAndroid Build Coastguard Worker 	    if (result != 0)
232*7304104dSAndroid Build Coastguard Worker 	      return fail (dwfl, result, _("cannot load kernel symbols"), state);
233*7304104dSAndroid Build Coastguard Worker 	    result = INTUSE(dwfl_linux_kernel_report_modules) (dwfl);
234*7304104dSAndroid Build Coastguard Worker 	    if (result != 0)
235*7304104dSAndroid Build Coastguard Worker 	      /* Non-fatal to have no modules since we do have the kernel.  */
236*7304104dSAndroid Build Coastguard Worker 	      argp_failure (state, 0, result, _("cannot find kernel modules"));
237*7304104dSAndroid Build Coastguard Worker 	    opt->dwfl = dwfl;
238*7304104dSAndroid Build Coastguard Worker 	  }
239*7304104dSAndroid Build Coastguard Worker 	else
240*7304104dSAndroid Build Coastguard Worker 	  goto toomany;
241*7304104dSAndroid Build Coastguard Worker       }
242*7304104dSAndroid Build Coastguard Worker       break;
243*7304104dSAndroid Build Coastguard Worker 
244*7304104dSAndroid Build Coastguard Worker     case 'K':
245*7304104dSAndroid Build Coastguard Worker       {
246*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
247*7304104dSAndroid Build Coastguard Worker 	if (opt->dwfl == NULL)
248*7304104dSAndroid Build Coastguard Worker 	  {
249*7304104dSAndroid Build Coastguard Worker 	    Dwfl *dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
250*7304104dSAndroid Build Coastguard Worker 	    int result = INTUSE(dwfl_linux_kernel_report_offline) (dwfl, arg,
251*7304104dSAndroid Build Coastguard Worker 								   NULL);
252*7304104dSAndroid Build Coastguard Worker 	    if (result != 0)
253*7304104dSAndroid Build Coastguard Worker 	      return fail (dwfl, result, _("cannot find kernel or modules"), state);
254*7304104dSAndroid Build Coastguard Worker 	    opt->dwfl = dwfl;
255*7304104dSAndroid Build Coastguard Worker 	  }
256*7304104dSAndroid Build Coastguard Worker 	else
257*7304104dSAndroid Build Coastguard Worker 	  goto toomany;
258*7304104dSAndroid Build Coastguard Worker       }
259*7304104dSAndroid Build Coastguard Worker       break;
260*7304104dSAndroid Build Coastguard Worker 
261*7304104dSAndroid Build Coastguard Worker     case ARGP_KEY_SUCCESS:
262*7304104dSAndroid Build Coastguard Worker       {
263*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
264*7304104dSAndroid Build Coastguard Worker 	Dwfl *dwfl = opt->dwfl;
265*7304104dSAndroid Build Coastguard Worker 
266*7304104dSAndroid Build Coastguard Worker 	if (dwfl == NULL)
267*7304104dSAndroid Build Coastguard Worker 	  {
268*7304104dSAndroid Build Coastguard Worker 	    /* Default if no -e, -p, or -k, is "-e a.out".  */
269*7304104dSAndroid Build Coastguard Worker 	    arg = "a.out";
270*7304104dSAndroid Build Coastguard Worker 	    dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
271*7304104dSAndroid Build Coastguard Worker 	    if (INTUSE(dwfl_report_offline) (dwfl, "", arg, -1) == NULL)
272*7304104dSAndroid Build Coastguard Worker 	      return fail (dwfl, -1, arg, state);
273*7304104dSAndroid Build Coastguard Worker 	    opt->dwfl = dwfl;
274*7304104dSAndroid Build Coastguard Worker 	  }
275*7304104dSAndroid Build Coastguard Worker 
276*7304104dSAndroid Build Coastguard Worker 	if (opt->core)
277*7304104dSAndroid Build Coastguard Worker 	  {
278*7304104dSAndroid Build Coastguard Worker 	    int fd = open (opt->core, O_RDONLY);
279*7304104dSAndroid Build Coastguard Worker 	    if (fd < 0)
280*7304104dSAndroid Build Coastguard Worker 	      {
281*7304104dSAndroid Build Coastguard Worker 		int code = errno;
282*7304104dSAndroid Build Coastguard Worker 		argp_failure (state, EXIT_FAILURE, code,
283*7304104dSAndroid Build Coastguard Worker 			      "cannot open '%s'", opt->core);
284*7304104dSAndroid Build Coastguard Worker 		return code;
285*7304104dSAndroid Build Coastguard Worker 	      }
286*7304104dSAndroid Build Coastguard Worker 
287*7304104dSAndroid Build Coastguard Worker 	    Elf *core;
288*7304104dSAndroid Build Coastguard Worker 	    Dwfl_Error error = __libdw_open_file (&fd, &core, true, false);
289*7304104dSAndroid Build Coastguard Worker 	    if (error != DWFL_E_NOERROR)
290*7304104dSAndroid Build Coastguard Worker 	      {
291*7304104dSAndroid Build Coastguard Worker 		argp_failure (state, EXIT_FAILURE, 0,
292*7304104dSAndroid Build Coastguard Worker 			      _("cannot read ELF core file: %s"),
293*7304104dSAndroid Build Coastguard Worker 			      INTUSE(dwfl_errmsg) (error));
294*7304104dSAndroid Build Coastguard Worker 		return error == DWFL_E_ERRNO ? errno : EIO;
295*7304104dSAndroid Build Coastguard Worker 	      }
296*7304104dSAndroid Build Coastguard Worker 
297*7304104dSAndroid Build Coastguard Worker 	    int result = INTUSE(dwfl_core_file_report) (dwfl, core, opt->e);
298*7304104dSAndroid Build Coastguard Worker 	    if (result < 0)
299*7304104dSAndroid Build Coastguard Worker 	      {
300*7304104dSAndroid Build Coastguard Worker 		elf_end (core);
301*7304104dSAndroid Build Coastguard Worker 		close (fd);
302*7304104dSAndroid Build Coastguard Worker 		return fail (dwfl, result, opt->core, state);
303*7304104dSAndroid Build Coastguard Worker 	      }
304*7304104dSAndroid Build Coastguard Worker 
305*7304104dSAndroid Build Coastguard Worker 	    /* Non-fatal to not be able to attach to core, ignore error.  */
306*7304104dSAndroid Build Coastguard Worker 	    INTUSE(dwfl_core_file_attach) (dwfl, core);
307*7304104dSAndroid Build Coastguard Worker 
308*7304104dSAndroid Build Coastguard Worker 	    /* Store core Elf and fd in Dwfl to expose with dwfl_end.  */
309*7304104dSAndroid Build Coastguard Worker 	    if (dwfl->user_core == NULL)
310*7304104dSAndroid Build Coastguard Worker 	      {
311*7304104dSAndroid Build Coastguard Worker 		dwfl->user_core = calloc (1, sizeof (struct Dwfl_User_Core));
312*7304104dSAndroid Build Coastguard Worker 		if (dwfl->user_core == NULL)
313*7304104dSAndroid Build Coastguard Worker 		  {
314*7304104dSAndroid Build Coastguard Worker 		    argp_failure (state, EXIT_FAILURE, 0,
315*7304104dSAndroid Build Coastguard Worker 				  _("Not enough memory"));
316*7304104dSAndroid Build Coastguard Worker 		    return ENOMEM;
317*7304104dSAndroid Build Coastguard Worker 		  }
318*7304104dSAndroid Build Coastguard Worker 	      }
319*7304104dSAndroid Build Coastguard Worker 	    dwfl->user_core->core = core;
320*7304104dSAndroid Build Coastguard Worker 	    dwfl->user_core->fd = fd;
321*7304104dSAndroid Build Coastguard Worker 
322*7304104dSAndroid Build Coastguard Worker 	    if (result == 0)
323*7304104dSAndroid Build Coastguard Worker 	      {
324*7304104dSAndroid Build Coastguard Worker 		argp_failure (state, EXIT_FAILURE, 0,
325*7304104dSAndroid Build Coastguard Worker 			      _("No modules recognized in core file"));
326*7304104dSAndroid Build Coastguard Worker 		return ENOENT;
327*7304104dSAndroid Build Coastguard Worker 	      }
328*7304104dSAndroid Build Coastguard Worker 	  }
329*7304104dSAndroid Build Coastguard Worker 	else if (opt->e)
330*7304104dSAndroid Build Coastguard Worker 	  {
331*7304104dSAndroid Build Coastguard Worker 	    if (INTUSE(dwfl_report_offline) (dwfl, "", opt->e, -1) == NULL)
332*7304104dSAndroid Build Coastguard Worker 	      return fail (dwfl, -1, opt->e, state);
333*7304104dSAndroid Build Coastguard Worker 	  }
334*7304104dSAndroid Build Coastguard Worker 
335*7304104dSAndroid Build Coastguard Worker 	/* One of the three flavors has done dwfl_begin and some reporting
336*7304104dSAndroid Build Coastguard Worker 	   if we got here.  Tie up the Dwfl and return it to the caller of
337*7304104dSAndroid Build Coastguard Worker 	   argp_parse.  */
338*7304104dSAndroid Build Coastguard Worker 
339*7304104dSAndroid Build Coastguard Worker 	int result = INTUSE(dwfl_report_end) (dwfl, NULL, NULL);
340*7304104dSAndroid Build Coastguard Worker 	if (result != 0)
341*7304104dSAndroid Build Coastguard Worker 	  return fail (dwfl, -1, arg, state);
342*7304104dSAndroid Build Coastguard Worker 
343*7304104dSAndroid Build Coastguard Worker 	/* Update the input all along, so a parent parser can see it.
344*7304104dSAndroid Build Coastguard Worker 	   As we free OPT the update below will be no longer active.  */
345*7304104dSAndroid Build Coastguard Worker 	*(Dwfl **) state->input = dwfl;
346*7304104dSAndroid Build Coastguard Worker 	free (opt);
347*7304104dSAndroid Build Coastguard Worker 	state->hook = NULL;
348*7304104dSAndroid Build Coastguard Worker       }
349*7304104dSAndroid Build Coastguard Worker       break;
350*7304104dSAndroid Build Coastguard Worker 
351*7304104dSAndroid Build Coastguard Worker     case ARGP_KEY_ERROR:
352*7304104dSAndroid Build Coastguard Worker       {
353*7304104dSAndroid Build Coastguard Worker 	struct parse_opt *opt = state->hook;
354*7304104dSAndroid Build Coastguard Worker 	dwfl_end (opt->dwfl);
355*7304104dSAndroid Build Coastguard Worker 	free (opt);
356*7304104dSAndroid Build Coastguard Worker 	state->hook = NULL;
357*7304104dSAndroid Build Coastguard Worker       }
358*7304104dSAndroid Build Coastguard Worker       break;
359*7304104dSAndroid Build Coastguard Worker 
360*7304104dSAndroid Build Coastguard Worker     default:
361*7304104dSAndroid Build Coastguard Worker       return ARGP_ERR_UNKNOWN;
362*7304104dSAndroid Build Coastguard Worker     }
363*7304104dSAndroid Build Coastguard Worker 
364*7304104dSAndroid Build Coastguard Worker   /* Update the input all along, so a parent parser can see it.  */
365*7304104dSAndroid Build Coastguard Worker   struct parse_opt *opt = state->hook;
366*7304104dSAndroid Build Coastguard Worker   if (opt)
367*7304104dSAndroid Build Coastguard Worker     *(Dwfl **) state->input = opt->dwfl;
368*7304104dSAndroid Build Coastguard Worker 
369*7304104dSAndroid Build Coastguard Worker   return 0;
370*7304104dSAndroid Build Coastguard Worker }
371*7304104dSAndroid Build Coastguard Worker 
372*7304104dSAndroid Build Coastguard Worker static const struct argp libdwfl_argp =
373*7304104dSAndroid Build Coastguard Worker   { .options = options, .parser = parse_opt };
374*7304104dSAndroid Build Coastguard Worker 
375*7304104dSAndroid Build Coastguard Worker const struct argp *
dwfl_standard_argp(void)376*7304104dSAndroid Build Coastguard Worker dwfl_standard_argp (void)
377*7304104dSAndroid Build Coastguard Worker {
378*7304104dSAndroid Build Coastguard Worker   return &libdwfl_argp;
379*7304104dSAndroid Build Coastguard Worker }
380