- add pool_match_nevr() to match a single solvable's nevr against
[platform/upstream/libsolv.git] / src / solverdebug.c
1 /*
2  * Copyright (c) 2008, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * solverdebug.c
10  *
11  * debug functions for the SAT solver
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include "solver.h"
21 #include "bitmap.h"
22 #include "pool.h"
23 #include "util.h"
24 #include "evr.h"
25 #include "policy.h"
26
27
28 /*
29  * create obsoletesmap from solver decisions
30  *
31  * for solvables in installed repo:
32  *   0 - not obsoleted
33  *   p - one of the packages that obsolete us
34  * for all others:
35  *   n - number of packages this package obsoletes
36  *
37  */
38
39 Id *
40 solver_create_decisions_obsoletesmap(Solver *solv)
41 {
42   Pool *pool = solv->pool;
43   Repo *installed = solv->installed;
44   Id p, *obsoletesmap = NULL;
45   int i;
46   Solvable *s;
47
48   obsoletesmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
49   if (installed)
50     {
51       for (i = 0; i < solv->decisionq.count; i++)
52         {
53           Id *pp, n;
54
55           n = solv->decisionq.elements[i];
56           if (n < 0)
57             continue;
58           if (n == SYSTEMSOLVABLE)
59             continue;
60           s = pool->solvables + n;
61           if (s->repo == installed)             /* obsoletes don't count for already installed packages */
62             continue;
63           FOR_PROVIDES(p, pp, s->name)
64             {
65               if (!solv->implicitobsoleteusesprovides && s->name != pool->solvables[p].name)
66                 continue;
67               if (pool->solvables[p].repo == installed && !obsoletesmap[p])
68                 {
69                   obsoletesmap[p] = n;
70                   obsoletesmap[n]++;
71                 }
72             }
73         }
74       for (i = 0; i < solv->decisionq.count; i++)
75         {
76           Id obs, *obsp;
77           Id *pp, n;
78
79           n = solv->decisionq.elements[i];
80           if (n < 0)
81             continue;
82           if (n == SYSTEMSOLVABLE)
83             continue;
84           s = pool->solvables + n;
85           if (s->repo == installed)             /* obsoletes don't count for already installed packages */
86             continue;
87           if (!s->obsoletes)
88             continue;
89           obsp = s->repo->idarraydata + s->obsoletes;
90           while ((obs = *obsp++) != 0)
91             {
92               FOR_PROVIDES(p, pp, obs)
93                 {
94                   if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
95                     continue;
96                   if (pool->solvables[p].repo == installed && !obsoletesmap[p])
97                     {
98                       obsoletesmap[p] = n;
99                       obsoletesmap[n]++;
100                     }
101                 }
102             }
103         }
104     }
105   return obsoletesmap;
106 }
107
108 void
109 solver_printruleelement(Solver *solv, int type, Rule *r, Id v)
110 {
111   Pool *pool = solv->pool;
112   Solvable *s;
113   if (v < 0)
114     {
115       s = pool->solvables + -v;
116       POOL_DEBUG(type, "    !%s [%d]", solvable2str(pool, s), -v);
117     }
118   else
119     {
120       s = pool->solvables + v;
121       POOL_DEBUG(type, "    %s [%d]", solvable2str(pool, s), v);
122     }
123   if (r)
124     {
125       if (r->w1 == v)
126         POOL_DEBUG(type, " (w1)");
127       if (r->w2 == v)
128         POOL_DEBUG(type, " (w2)");
129     }
130   if (solv->decisionmap[s - pool->solvables] > 0)
131     POOL_DEBUG(type, " Install.level%d", solv->decisionmap[s - pool->solvables]);
132   if (solv->decisionmap[s - pool->solvables] < 0)
133     POOL_DEBUG(type, " Conflict.level%d", -solv->decisionmap[s - pool->solvables]);
134   POOL_DEBUG(type, "\n");
135 }
136
137
138 /*
139  * print rule
140  */
141
142 void
143 solver_printrule(Solver *solv, int type, Rule *r)
144 {
145   Pool *pool = solv->pool;
146   int i;
147   Id d, v;
148
149   if (r >= solv->rules && r < solv->rules + solv->nrules)   /* r is a solver rule */
150     POOL_DEBUG(type, "Rule #%d:", (int)(r - solv->rules));
151   else
152     POOL_DEBUG(type, "Rule:");                 /* r is any rule */
153   if (r && r->d < 0)
154     POOL_DEBUG(type, " (disabled)");
155   POOL_DEBUG(type, "\n");
156   d = r->d < 0 ? -r->d - 1 : r->d;
157   for (i = 0; ; i++)
158     {
159       if (i == 0)
160           /* print direct literal */
161         v = r->p;
162       else if (!d)
163         {
164           if (i == 2)
165             break;
166           /* binary rule --> print w2 as second literal */
167           v = r->w2;
168         }
169       else
170           /* every other which is in d */
171         v = solv->pool->whatprovidesdata[d + i - 1];
172       if (v == ID_NULL)
173         break;
174       solver_printruleelement(solv, type, r, v);
175     }
176   POOL_DEBUG(type, "    next rules: %d %d\n", r->n1, r->n2);
177 }
178
179 void
180 solver_printruleclass(Solver *solv, int type, Rule *r)
181 {
182   Pool *pool = solv->pool;
183   Id p = r - solv->rules;
184   assert(p >= 0);
185   if (p < solv->learntrules)
186     if (MAPTST(&solv->weakrulemap, p))
187       POOL_DEBUG(type, "WEAK ");
188   if (p >= solv->learntrules)
189     POOL_DEBUG(type, "LEARNT ");
190   else if (p >= solv->jobrules && p < solv->jobrules_end)
191     POOL_DEBUG(type, "JOB ");
192   else if (p >= solv->updaterules && p < solv->updaterules_end)
193     POOL_DEBUG(type, "UPDATE ");
194   else if (p >= solv->featurerules && p < solv->featurerules_end)
195     POOL_DEBUG(type, "FEATURE ");
196   solver_printrule(solv, type, r);
197 }
198
199 void
200 solver_printproblem(Solver *solv, Id v)
201 {
202   Pool *pool = solv->pool;
203   int i;
204   Rule *r;
205   Id *jp;
206
207   if (v > 0)
208     solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, solv->rules + v);
209   else
210     {
211       v = -(v + 1);
212       POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "JOB %d\n", v);
213       jp = solv->ruletojob.elements;
214       for (i = solv->jobrules, r = solv->rules + i; i < solv->learntrules; i++, r++, jp++)
215         if (*jp == v)
216           {
217             POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "- ");
218             solver_printrule(solv, SAT_DEBUG_SOLUTIONS, r);
219           }
220       POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "ENDJOB\n");
221     }
222 }
223
224 void
225 solver_printwatches(Solver *solv, int type)
226 {
227   Pool *pool = solv->pool;
228   int counter;
229
230   POOL_DEBUG(type, "Watches: \n");
231   for (counter = -(pool->nsolvables - 1); counter < pool->nsolvables; counter++)
232     POOL_DEBUG(type, "    solvable [%d] -- rule [%d]\n", counter, solv->watches[counter + pool->nsolvables]);
233 }
234
235 /*
236  * printdecisions
237  */
238
239 void
240 solver_printdecisions(Solver *solv)
241 {
242   Pool *pool = solv->pool;
243   Repo *installed = solv->installed;
244   Id p, *obsoletesmap = solver_create_decisions_obsoletesmap(solv);
245   int i;
246   Solvable *s;
247
248   IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
249     {
250       POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions -----\n");
251       for (i = 0; i < solv->decisionq.count; i++)
252         {
253           p = solv->decisionq.elements[i];
254           solver_printruleelement(solv, SAT_DEBUG_SCHUBI, 0, p);
255         }
256       POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions end -----\n");
257     }
258
259   /* print solvables to be erased */
260
261   if (installed)
262     {
263       FOR_REPO_SOLVABLES(installed, p, s)
264         {
265           if (solv->decisionmap[p] >= 0)
266             continue;
267           if (obsoletesmap[p])
268             continue;
269           POOL_DEBUG(SAT_DEBUG_RESULT, "erase   %s\n", solvable2str(pool, s));
270         }
271     }
272
273   /* print solvables to be installed */
274
275   for (i = 0; i < solv->decisionq.count; i++)
276     {
277       int j;
278       p = solv->decisionq.elements[i];
279       if (p < 0)
280         continue;
281       if (p == SYSTEMSOLVABLE)
282         continue;
283       s = pool->solvables + p;
284       if (installed && s->repo == installed)
285         continue;
286
287       if (!obsoletesmap[p])
288         {
289           POOL_DEBUG(SAT_DEBUG_RESULT, "install   %s", solvable2str(pool, s));
290         }
291       else
292         {
293           Id xp, *xpp;
294           FOR_PROVIDES(xp, xpp, s->name)
295             {
296               Solvable *s2 = pool->solvables + xp;
297               if (s2->name != s->name)
298                 continue;
299               if (evrcmp(pool, s->evr, s2->evr, EVRCMP_MATCH_RELEASE) < 0)
300                 break;
301             }
302           if (xp)
303             POOL_DEBUG(SAT_DEBUG_RESULT, "downgrade %s", solvable2str(pool, s));
304           else
305             POOL_DEBUG(SAT_DEBUG_RESULT, "upgrade   %s", solvable2str(pool, s));
306           POOL_DEBUG(SAT_DEBUG_RESULT, "  (obsoletes");
307           for (j = installed->start; j < installed->end; j++)
308             if (obsoletesmap[j] == p)
309               POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
310           POOL_DEBUG(SAT_DEBUG_RESULT, ")");
311         }
312       POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
313     }
314
315   sat_free(obsoletesmap);
316
317   if (solv->recommendations.count)
318     {
319       POOL_DEBUG(SAT_DEBUG_RESULT, "\nrecommended packages:\n");
320       for (i = 0; i < solv->recommendations.count; i++)
321         {
322           s = pool->solvables + solv->recommendations.elements[i];
323           if (solv->decisionmap[solv->recommendations.elements[i]] > 0)
324             {
325               if (installed && s->repo == installed)
326                 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s (installed)\n", solvable2str(pool, s));
327               else
328                 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s (selected)\n", solvable2str(pool, s));
329             }
330           else
331             POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
332         }
333     }
334
335   if (solv->suggestions.count)
336     {
337       POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
338       for (i = 0; i < solv->suggestions.count; i++)
339         {
340           s = pool->solvables + solv->suggestions.elements[i];
341           if (solv->decisionmap[solv->suggestions.elements[i]] > 0)
342             {
343               if (installed && s->repo == installed)
344                 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s (installed)\n", solvable2str(pool, s));
345               else
346                 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s (selected)\n", solvable2str(pool, s));
347             }
348           else
349             POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
350         }
351     }
352 }
353
354 void
355 solver_printprobleminfo(Solver *solv, Queue *job, Id problem)
356 {
357   Pool *pool = solv->pool;
358   Id probr;
359   Id dep, source, target;
360   Solvable *s, *s2;
361
362   probr = solver_findproblemrule(solv, problem);
363   switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
364     {
365     case SOLVER_PROBLEM_UPDATE_RULE:
366       s = pool_id2solvable(pool, source);
367       POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
368       return;
369     case SOLVER_PROBLEM_JOB_RULE:
370       POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
371       return;
372     case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
373       POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
374       return;
375     case SOLVER_PROBLEM_NOT_INSTALLABLE:
376       s = pool_id2solvable(pool, source);
377       POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
378       return;
379     case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
380       s = pool_id2solvable(pool, source);
381       POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
382       return;
383     case SOLVER_PROBLEM_SAME_NAME:
384       s = pool_id2solvable(pool, source);
385       s2 = pool_id2solvable(pool, target);
386       POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
387       return;
388     case SOLVER_PROBLEM_PACKAGE_CONFLICT:
389       s = pool_id2solvable(pool, source);
390       s2 = pool_id2solvable(pool, target);
391       POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
392       return;
393     case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
394       s = pool_id2solvable(pool, source);
395       s2 = pool_id2solvable(pool, target);
396       POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
397       return;
398     case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
399       s = pool_id2solvable(pool, source);
400       POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
401       return;
402     case SOLVER_PROBLEM_SELF_CONFLICT:
403       s = pool_id2solvable(pool, source);
404       POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by itself\n", solvable2str(pool, s), dep2str(pool, dep));
405       return;
406     }
407 }
408
409 void
410 solver_printsolutions(Solver *solv, Queue *job)
411 {
412   Pool *pool = solv->pool;
413   int pcnt;
414   Id p, rp, how, what;
415   Id problem, solution, element;
416   Solvable *s, *sd;
417
418   POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
419   pcnt = 1;
420   problem = 0;
421   while ((problem = solver_next_problem(solv, problem)) != 0)
422     {
423       POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
424       POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
425       solver_printprobleminfo(solv, job, problem);
426       POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
427       solution = 0;
428       while ((solution = solver_next_solution(solv, problem, solution)) != 0)
429         {
430           element = 0;
431           while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
432             {
433               if (p == 0)
434                 {
435                   /* job, rp is index into job queue */
436                   how = job->elements[rp - 1] & ~SOLVER_WEAK;
437                   what = job->elements[rp];
438                   switch (how)
439                     {
440                     case SOLVER_INSTALL_SOLVABLE:
441                       s = pool->solvables + what;
442                       if (solv->installed && s->repo == solv->installed)
443                         POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
444                       else
445                         POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
446                       break;
447                     case SOLVER_ERASE_SOLVABLE:
448                       s = pool->solvables + what;
449                       if (solv->installed && s->repo == solv->installed)
450                         POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
451                       else
452                         POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
453                       break;
454                     case SOLVER_INSTALL_SOLVABLE_NAME:
455                       POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", dep2str(pool, what));
456                       break;
457                     case SOLVER_ERASE_SOLVABLE_NAME:
458                       POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", dep2str(pool, what));
459                       break;
460                     case SOLVER_INSTALL_SOLVABLE_PROVIDES:
461                       POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
462                       break;
463                     case SOLVER_ERASE_SOLVABLE_PROVIDES:
464                       POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
465                       break;
466                     case SOLVER_INSTALL_SOLVABLE_UPDATE:
467                       s = pool->solvables + what;
468                       POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
469                       break;
470                     default:
471                       POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
472                       break;
473                     }
474                 }
475               else
476                 {
477                   /* policy, replace p with rp */
478                   s = pool->solvables + p;
479                   sd = rp ? pool->solvables + rp : 0;
480                   if (sd)
481                     {
482                       int gotone = 0;
483                       if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr, EVRCMP_MATCH_RELEASE) > 0)
484                         {
485                           POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
486                           gotone = 1;
487                         }
488                       if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(solv, s, sd))
489                         {
490                           POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
491                           gotone = 1;
492                         }
493                       if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(solv, s, sd))
494                         {
495                           if (sd->vendor)
496                             POOL_DEBUG(SAT_DEBUG_RESULT, "- allow vendor change from '%s' (%s) to '%s' (%s)\n", id2str(pool, s->vendor), solvable2str(pool, s), id2str(pool, sd->vendor), solvable2str(pool, sd));
497                           else
498                             POOL_DEBUG(SAT_DEBUG_RESULT, "- allow vendor change from '%s' (%s) to no vendor (%s)\n", id2str(pool, s->vendor), solvable2str(pool, s), solvable2str(pool, sd));
499                           gotone = 1;
500                         }
501                       if (!gotone)
502                         POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
503                     }
504                   else
505                     {
506                       POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
507                     }
508
509                 }
510             }
511           POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
512         }
513     }
514 }
515