summaryrefslogtreecommitdiff
path: root/libdwfl/debuginfod-client.c
blob: 882a5efff9bbc622c0fcce90c021b18530b341ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Try to get an ELF or debug file through the debuginfod.
   Copyright (C) 2019 Red Hat, Inc.
   Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version

   or both in parallel, as here.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "libdwflP.h"

#ifdef ENABLE_LIBDEBUGINFOD

#include "debuginfod.h"

#include <pthread.h>
#include <dlfcn.h>

static __typeof__ (debuginfod_begin) *fp_debuginfod_begin;
static __typeof__ (debuginfod_find_executable) *fp_debuginfod_find_executable;
static __typeof__ (debuginfod_find_debuginfo) *fp_debuginfod_find_debuginfo;
static __typeof__ (debuginfod_end) *fp_debuginfod_end;

static void __libdwfl_debuginfod_init (void);

static pthread_once_t init_control = PTHREAD_ONCE_INIT;

/* NB: this is slightly thread-unsafe */

debuginfod_client *
dwfl_get_debuginfod_client (Dwfl *dwfl)
{
  if (dwfl->debuginfod != NULL)
    return dwfl->debuginfod;

  pthread_once (&init_control, __libdwfl_debuginfod_init);

  if (fp_debuginfod_begin != NULL)
    {
      dwfl->debuginfod = (*fp_debuginfod_begin) ();
      return dwfl->debuginfod;
    }

  return NULL;
}
INTDEF(dwfl_get_debuginfod_client)

int
__libdwfl_debuginfod_find_executable (Dwfl *dwfl,
				      const unsigned char *build_id_bits,
				      size_t build_id_len)
{
  int fd = -1;
  if (build_id_len > 0)
    {
      debuginfod_client *c = INTUSE (dwfl_get_debuginfod_client) (dwfl);
      if (c != NULL)
	fd = (*fp_debuginfod_find_executable) (c, build_id_bits,
					       build_id_len, NULL);
    }

  return fd;
}

int
__libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl,
				     const unsigned char *build_id_bits,
				     size_t build_id_len)
{
  int fd = -1;
  if (build_id_len > 0)
    {
      debuginfod_client *c = INTUSE (dwfl_get_debuginfod_client) (dwfl);
      if (c != NULL)
	fd = (*fp_debuginfod_find_debuginfo) (c, build_id_bits,
					      build_id_len, NULL);
    }

  return fd;
}

void
__libdwfl_debuginfod_end (debuginfod_client *c)
{
  if (c != NULL)
    (*fp_debuginfod_end) (c);
}

/* Try to get the libdebuginfod library functions.
   Only needs to be called once from dwfl_get_debuginfod_client.  */
static void
__libdwfl_debuginfod_init (void)
{
  void *debuginfod_so = dlopen(DEBUGINFOD_SONAME, RTLD_LAZY);

  if (debuginfod_so != NULL)
    {
      fp_debuginfod_begin = dlsym (debuginfod_so, "debuginfod_begin");
      fp_debuginfod_find_executable = dlsym (debuginfod_so,
					     "debuginfod_find_executable");
      fp_debuginfod_find_debuginfo = dlsym (debuginfod_so,
					    "debuginfod_find_debuginfo");
      fp_debuginfod_end = dlsym (debuginfod_so, "debuginfod_end");

      /* We either get them all, or we get none.  */
      if (fp_debuginfod_begin == NULL
	  || fp_debuginfod_find_executable == NULL
	  || fp_debuginfod_find_debuginfo == NULL
	  || fp_debuginfod_end == NULL)
	{
	  fp_debuginfod_begin = NULL;
	  fp_debuginfod_find_executable = NULL;
	  fp_debuginfod_find_debuginfo = NULL;
	  fp_debuginfod_end = NULL;
	  dlclose (debuginfod_so);
	}
    }
}

#else // ENABLE_LIBDEBUGINFOD

debuginfod_client *
dwfl_get_debuginfod_client (Dwfl *dummy __attribute__ ((unused)))
{
  return NULL;
}

#endif // ENABLE_LIBDEBUGINFOD