summaryrefslogtreecommitdiffstats
path: root/rpm_finder.cxx
blob: 779a9c36c277987cb1962dd97d0a5e1d5a09f1eb (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// systemtap debuginfo rpm finder
// Copyright (C) 2009 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "config.h"
#include "session.h"
#include "rpm_finder.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <cerrno>
#include <cstdlib>

using namespace std;

#ifdef HAVE_LIBRPM

extern "C" {

#define _RPM_4_4_COMPAT
#include <string.h>
#include <rpm/rpmlib.h>
#include <rpm/rpmts.h>
#include <rpm/rpmdb.h>
#include <rpm/header.h>

#ifndef xfree
#define xfree free
#endif

}

/* Returns the count of newly added rpms.  */
/* based on the code in F11 gdb-6.8.50.20090302 source rpm */

static int
missing_rpm_enlist (systemtap_session& sess, const char *filename)
{
  static int rpm_init_done = 0;
  rpmts ts;
  rpmdbMatchIterator mi;
  int count = 0;

  if (filename == NULL)
    return 0;

  if (!rpm_init_done)
    {
      static int init_tried;

      /* Already failed the initialization before?  */
      if (init_tried)
      	return 0;
      init_tried = 1;

      if (rpmReadConfigFiles(NULL, NULL) != 0)
	{
	  cerr << "Error reading the rpm configuration files" << endl;
	  return 0;
	}

      rpm_init_done = 1;
    }

  ts = rpmtsCreate();

  mi = rpmtsInitIterator(ts, RPMTAG_BASENAMES, filename, 0);
  if (mi != NULL)
    {
      for (;;)
	{
	  Header h;
	  char *debuginfo, *s, *s2;
	  errmsg_t err;
	  size_t srcrpmlen = sizeof (".src.rpm") - 1;
	  size_t debuginfolen = sizeof ("-debuginfo") - 1;
	  rpmdbMatchIterator mi_debuginfo;

	  h = rpmdbNextIterator(mi);
	  if (h == NULL)
	    break;

	  /* Verify the debuginfo file is not already installed.  */

	  debuginfo = headerSprintf(h, "%{sourcerpm}-debuginfo.%{arch}",
				      rpmTagTable, rpmHeaderFormats, &err);

	  if (!debuginfo)
	    {
	      cerr << "Error querying the rpm file `" << filename << "': "
		   << err << endl;
	      continue;
	    }
	  /* s = `.src.rpm-debuginfo.%{arch}' */
	  s = strrchr (debuginfo, '-') - srcrpmlen;
	  s2 = NULL;
	  if (s > debuginfo && memcmp (s, ".src.rpm", srcrpmlen) == 0)
	    {
	      /* s2 = `-%{release}.src.rpm-debuginfo.%{arch}' */
	      s2 = (char *) memrchr (debuginfo, '-', s - debuginfo);
	    }
	  if (s2)
	    {
	      /* s2 = `-%{version}-%{release}.src.rpm-debuginfo.%{arch}' */
	      s2 = (char *) memrchr (debuginfo, '-', s2 - debuginfo);
	    }
	  if (!s2)
	    {
	      cerr << "Error querying the rpm file `" << filename 
		   << "': " << debuginfo << endl;
	      xfree (debuginfo);
	      continue;
	    }
	  /* s = `.src.rpm-debuginfo.%{arch}' */
	  /* s2 = `-%{version}-%{release}.src.rpm-debuginfo.%{arch}' */
	  memmove (s2 + debuginfolen, s2, s - s2);
	  memcpy (s2, "-debuginfo", debuginfolen);
	  /* s = `XXXX.%{arch}' */
	  /* strlen ("XXXX") == srcrpmlen + debuginfolen */
	  /* s2 = `-debuginfo-%{version}-%{release}XX.%{arch}' */
	  /* strlen ("XX") == srcrpmlen */
	  memmove (s + debuginfolen, s + srcrpmlen + debuginfolen,
		   strlen (s + srcrpmlen + debuginfolen) + 1);
	  /* s = `-debuginfo-%{version}-%{release}.%{arch}' */

	  /* RPMDBI_PACKAGES requires keylen == sizeof (int).  */
	  /* RPMDBI_LABEL is an interface for NVR-based dbiFindByLabel().  */
	  mi_debuginfo = rpmtsInitIterator(ts, (rpmTag)  RPMDBI_LABEL,
					      debuginfo, 0);
	  xfree (debuginfo);
	  if (mi_debuginfo)
	    {
	      rpmdbFreeIterator(mi_debuginfo);
	      count = 0;
	      break;
	    }

	  /* The allocated memory gets utilized below for MISSING_RPM_HASH.  */
	  debuginfo = headerSprintf(h,
				      "%{name}-%{version}-%{release}.%{arch}",
				      rpmTagTable, rpmHeaderFormats, &err);
	  if (!debuginfo)
	    {
	      cerr << "Error querying the rpm file `" << filename 
		   << "': " << err << endl;
	      continue;
	    }

	  /* Base package name for `debuginfo-install'.  We do not use the
	     `yum' command directly as the line
		 yum --enablerepo='*-debuginfo' install NAME-debuginfo.ARCH
	     would be more complicated than just:
		 debuginfo-install NAME-VERSION-RELEASE.ARCH
	     Do not supply the rpm base name (derived from .src.rpm name) as
	     debuginfo-install is unable to install the debuginfo package if
	     the base name PKG binary rpm is not installed while for example
	     PKG-libs would be installed (RH Bug 467901).
	     FUTURE: After multiple debuginfo versions simultaneously installed
	     get supported the support for the VERSION-RELEASE tags handling
	     may need an update.  */

	  sess.rpms_to_install.insert(debuginfo);
	  count++;
	}

      rpmdbFreeIterator(mi);
    }

  rpmtsFree(ts);

  return count;
}

#endif	/* HAVE_LIBRPM */

void
missing_rpm_list_print (systemtap_session &sess)
{
#ifdef HAVE_LIBRPM
  if (sess.rpms_to_install.size() > 0) {
    cerr << "Missing separate debuginfos, use: debuginfo-install ";
    for (set<std::string>::iterator it=sess.rpms_to_install.begin();
	 it !=sess.rpms_to_install.end(); it++)
      cerr <<  *it << " ";
    cerr << endl;
  }
#endif
}

int
find_debug_rpms (systemtap_session &sess, const char * filename)
{
#ifdef HAVE_LIBRPM
  return missing_rpm_enlist (sess, filename);
#else
  return 0;
#endif
}