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