summaryrefslogtreecommitdiff
path: root/libdwfl/debuginfod-client.c
blob: ee604ad9a85fb62f333aad4b4170e85f3100658a (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
/* Try to get an ELF or debug file through the debuginfod.
   Copyright (C) 2019 Red Hat, Inc.
   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"
#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;

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

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

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

  return NULL;
}

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 = get_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 = get_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 to make sure
   everything is initialized early.  */
void __attribute__ ((constructor))
__libdwfl_debuginfod_init (void)
{
  void *debuginfod_so = dlopen("libdebuginfod-" VERSION ".so", RTLD_LAZY);

  if (debuginfod_so == NULL)
    debuginfod_so = dlopen("libdebuginfod.so", 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);
	}
    }
}