0b2879b08dfba5cba2a05b7edd9b44b5655defd3
[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 "solver_private.h"
22 #include "solverdebug.h"
23 #include "bitmap.h"
24 #include "pool.h"
25 #include "poolarch.h"
26 #include "util.h"
27 #include "evr.h"
28 #include "policy.h"
29
30
31 void
32 solver_printruleelement(Solver *solv, int type, Rule *r, Id v)
33 {
34   Pool *pool = solv->pool;
35   Solvable *s;
36   if (v < 0)
37     {
38       s = pool->solvables + -v;
39       POOL_DEBUG(type, "    !%s [%d]", pool_solvable2str(pool, s), -v);
40     }
41   else
42     {
43       s = pool->solvables + v;
44       POOL_DEBUG(type, "    %s [%d]", pool_solvable2str(pool, s), v);
45     }
46   if (pool->installed && s->repo == pool->installed)
47     POOL_DEBUG(type, "I");
48   if (r)
49     {
50       if (r->w1 == v)
51         POOL_DEBUG(type, " (w1)");
52       if (r->w2 == v)
53         POOL_DEBUG(type, " (w2)");
54     }
55   if (solv->decisionmap[s - pool->solvables] > 0)
56     POOL_DEBUG(type, " Install.level%d", solv->decisionmap[s - pool->solvables]);
57   if (solv->decisionmap[s - pool->solvables] < 0)
58     POOL_DEBUG(type, " Conflict.level%d", -solv->decisionmap[s - pool->solvables]);
59   POOL_DEBUG(type, "\n");
60 }
61
62
63 /*
64  * print rule
65  */
66
67 void
68 solver_printrule(Solver *solv, int type, Rule *r)
69 {
70   Pool *pool = solv->pool;
71   int i;
72   Id d, v;
73
74   if (r >= solv->rules && r < solv->rules + solv->nrules)   /* r is a solver rule */
75     POOL_DEBUG(type, "Rule #%d:", (int)(r - solv->rules));
76   else
77     POOL_DEBUG(type, "Rule:");                 /* r is any rule */
78   if (r->d < 0)
79     POOL_DEBUG(type, " (disabled)");
80   POOL_DEBUG(type, "\n");
81   d = r->d < 0 ? -r->d - 1 : r->d;
82   for (i = 0; ; i++)
83     {
84       if (i == 0)
85           /* print direct literal */
86         v = r->p;
87       else if (!d)
88         {
89           if (i == 2)
90             break;
91           /* binary rule --> print w2 as second literal */
92           v = r->w2;
93         }
94       else
95           /* every other which is in d */
96         v = solv->pool->whatprovidesdata[d + i - 1];
97       if (v == ID_NULL)
98         break;
99       solver_printruleelement(solv, type, r, v);
100     }
101   POOL_DEBUG(type, "    next rules: %d %d\n", r->n1, r->n2);
102 }
103
104 void
105 solver_printruleclass(Solver *solv, int type, Rule *r)
106 {
107   Pool *pool = solv->pool;
108   Id p = r - solv->rules;
109   assert(p >= 0);
110   if (p < solv->learntrules)
111     if (solv->weakrulemap.size && MAPTST(&solv->weakrulemap, p))
112       POOL_DEBUG(type, "WEAK ");
113   if (solv->learntrules && p >= solv->learntrules)
114     POOL_DEBUG(type, "LEARNT ");
115   else if (p >= solv->bestrules && p < solv->bestrules_end)
116     POOL_DEBUG(type, "BEST ");
117   else if (p >= solv->choicerules && p < solv->choicerules_end)
118     POOL_DEBUG(type, "CHOICE ");
119   else if (p >= solv->infarchrules && p < solv->infarchrules_end)
120     POOL_DEBUG(type, "INFARCH ");
121   else if (p >= solv->duprules && p < solv->duprules_end)
122     POOL_DEBUG(type, "DUP ");
123   else if (p >= solv->jobrules && p < solv->jobrules_end)
124     POOL_DEBUG(type, "JOB ");
125   else if (p >= solv->updaterules && p < solv->updaterules_end)
126     POOL_DEBUG(type, "UPDATE ");
127   else if (p >= solv->featurerules && p < solv->featurerules_end)
128     POOL_DEBUG(type, "FEATURE ");
129   else if (p >= solv->yumobsrules && p < solv->yumobsrules_end)
130     POOL_DEBUG(type, "YUMOBS ");
131   else if (p >= solv->blackrules && p < solv->blackrules_end)
132     POOL_DEBUG(type, "BLACK ");
133   else if (p >= solv->recommendsrules && p < solv->recommendsrules_end)
134     POOL_DEBUG(type, "RECOMMENDS ");
135   solver_printrule(solv, type, r);
136 }
137
138 void
139 solver_printproblem(Solver *solv, Id v)
140 {
141   Pool *pool = solv->pool;
142   int i;
143   Rule *r;
144   Id *jp;
145
146   if (v > 0)
147     solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, solv->rules + v);
148   else
149     {
150       v = -(v + 1);
151       POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "JOB %d\n", v);
152       jp = solv->ruletojob.elements;
153       for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
154         if (*jp == v)
155           {
156             POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "- ");
157             solver_printrule(solv, SOLV_DEBUG_SOLUTIONS, r);
158           }
159       POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "ENDJOB\n");
160     }
161 }
162
163 void
164 solver_printwatches(Solver *solv, int type)
165 {
166   Pool *pool = solv->pool;
167   int counter;
168
169   POOL_DEBUG(type, "Watches: \n");
170   for (counter = -(pool->nsolvables - 1); counter < pool->nsolvables; counter++)
171     POOL_DEBUG(type, "    solvable [%d] -- rule [%d]\n", counter, solv->watches[counter + pool->nsolvables]);
172 }
173
174 void
175 solver_printdecisionq(Solver *solv, int type)
176 {
177   Pool *pool = solv->pool;
178   int i;
179   Id p, why;
180
181   POOL_DEBUG(type, "Decisions:\n");
182   for (i = 0; i < solv->decisionq.count; i++)
183     {
184       p = solv->decisionq.elements[i];
185       if (p > 0)
186         POOL_DEBUG(type, "%d %d install  %s, ", i, solv->decisionmap[p], pool_solvid2str(pool, p));
187       else
188         POOL_DEBUG(type, "%d %d conflict %s, ", i, -solv->decisionmap[-p], pool_solvid2str(pool, -p));
189       why = solv->decisionq_why.elements[i];
190       if (why > 0)
191         {
192           POOL_DEBUG(type, "forced by ");
193           solver_printruleclass(solv, type, solv->rules + why);
194         }
195       else if (why < 0)
196         {
197           POOL_DEBUG(type, "chosen from ");
198           solver_printruleclass(solv, type, solv->rules - why);
199         }
200       else
201         POOL_DEBUG(type, "picked for some unknown reason.\n");
202     }
203 }
204
205 /*
206  * printdecisions
207  */
208
209 void
210 solver_printdecisions(Solver *solv)
211 {
212   Pool *pool = solv->pool;
213   Repo *installed = solv->installed;
214   Transaction *trans = solver_create_transaction(solv);
215   Id p, type;
216   int i, j;
217   Solvable *s;
218   Queue iq;
219   Queue recommendations;
220   Queue suggestions;
221   Queue orphaned;
222
223   POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
224   POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction:\n");
225
226   queue_init(&iq);
227   for (i = 0; i < trans->steps.count; i++)
228     {
229       p = trans->steps.elements[i];
230       s = pool->solvables + p;
231       type = transaction_type(trans, p, SOLVER_TRANSACTION_SHOW_ACTIVE|SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES|SOLVER_TRANSACTION_SHOW_MULTIINSTALL);
232       switch(type)
233         {
234         case SOLVER_TRANSACTION_MULTIINSTALL:
235           POOL_DEBUG(SOLV_DEBUG_RESULT, "  multi install %s", pool_solvable2str(pool, s));
236           break;
237         case SOLVER_TRANSACTION_MULTIREINSTALL:
238           POOL_DEBUG(SOLV_DEBUG_RESULT, "  multi reinstall %s", pool_solvable2str(pool, s));
239           break;
240         case SOLVER_TRANSACTION_INSTALL:
241           POOL_DEBUG(SOLV_DEBUG_RESULT, "  install   %s", pool_solvable2str(pool, s));
242           break;
243         case SOLVER_TRANSACTION_REINSTALL:
244           POOL_DEBUG(SOLV_DEBUG_RESULT, "  reinstall %s", pool_solvable2str(pool, s));
245           break;
246         case SOLVER_TRANSACTION_DOWNGRADE:
247           POOL_DEBUG(SOLV_DEBUG_RESULT, "  downgrade %s", pool_solvable2str(pool, s));
248           break;
249         case SOLVER_TRANSACTION_CHANGE:
250           POOL_DEBUG(SOLV_DEBUG_RESULT, "  change    %s", pool_solvable2str(pool, s));
251           break;
252         case SOLVER_TRANSACTION_UPGRADE:
253         case SOLVER_TRANSACTION_OBSOLETES:
254           POOL_DEBUG(SOLV_DEBUG_RESULT, "  upgrade   %s", pool_solvable2str(pool, s));
255           break;
256         case SOLVER_TRANSACTION_ERASE:
257           POOL_DEBUG(SOLV_DEBUG_RESULT, "  erase     %s", pool_solvable2str(pool, s));
258           break;
259         default:
260           break;
261         }
262       switch(type)
263         {
264         case SOLVER_TRANSACTION_INSTALL:
265         case SOLVER_TRANSACTION_ERASE:
266         case SOLVER_TRANSACTION_MULTIINSTALL:
267         case SOLVER_TRANSACTION_MULTIREINSTALL:
268           POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
269           break;
270         case SOLVER_TRANSACTION_REINSTALL:
271         case SOLVER_TRANSACTION_DOWNGRADE:
272         case SOLVER_TRANSACTION_CHANGE:
273         case SOLVER_TRANSACTION_UPGRADE:
274         case SOLVER_TRANSACTION_OBSOLETES:
275           transaction_all_obs_pkgs(trans, p, &iq);
276           if (iq.count)
277             {
278               POOL_DEBUG(SOLV_DEBUG_RESULT, "  (obsoletes");
279               for (j = 0; j < iq.count; j++)
280                 POOL_DEBUG(SOLV_DEBUG_RESULT, " %s", pool_solvid2str(pool, iq.elements[j]));
281               POOL_DEBUG(SOLV_DEBUG_RESULT, ")");
282             }
283           POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
284           break;
285         default:
286           break;
287         }
288     }
289   queue_free(&iq);
290
291   POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
292
293   queue_init(&recommendations);
294   queue_init(&suggestions);
295   queue_init(&orphaned);
296   solver_get_recommendations(solv, &recommendations, &suggestions, 0);
297   solver_get_orphaned(solv, &orphaned);
298   if (recommendations.count)
299     {
300       POOL_DEBUG(SOLV_DEBUG_RESULT, "recommended packages:\n");
301       for (i = 0; i < recommendations.count; i++)
302         {
303           s = pool->solvables + recommendations.elements[i];
304           if (solv->decisionmap[recommendations.elements[i]] > 0)
305             {
306               if (installed && s->repo == installed)
307                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (installed)\n", pool_solvable2str(pool, s));
308               else
309                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (selected)\n", pool_solvable2str(pool, s));
310             }
311           else
312             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s\n", pool_solvable2str(pool, s));
313         }
314       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
315     }
316
317   if (suggestions.count)
318     {
319       POOL_DEBUG(SOLV_DEBUG_RESULT, "suggested packages:\n");
320       for (i = 0; i < suggestions.count; i++)
321         {
322           s = pool->solvables + suggestions.elements[i];
323           if (solv->decisionmap[suggestions.elements[i]] > 0)
324             {
325               if (installed && s->repo == installed)
326                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (installed)\n", pool_solvable2str(pool, s));
327               else
328                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (selected)\n", pool_solvable2str(pool, s));
329             }
330           else
331             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s\n", pool_solvable2str(pool, s));
332         }
333       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
334     }
335   if (orphaned.count)
336     {
337       POOL_DEBUG(SOLV_DEBUG_RESULT, "orphaned packages:\n");
338       for (i = 0; i < orphaned.count; i++)
339         {
340           s = pool->solvables + orphaned.elements[i];
341           if (solv->decisionmap[solv->orphaned.elements[i]] > 0)
342             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (kept)\n", pool_solvable2str(pool, s));
343           else
344             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (erased)\n", pool_solvable2str(pool, s));
345         }
346       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
347     }
348   queue_free(&recommendations);
349   queue_free(&suggestions);
350   queue_free(&orphaned);
351   transaction_free(trans);
352 }
353
354 static inline
355 const char *id2strnone(Pool *pool, Id id)
356 {
357   return !id || id == 1 ? "(none)" : pool_id2str(pool, id);
358 }
359
360 void
361 transaction_print(Transaction *trans)
362 {
363   Pool *pool = trans->pool;
364   Queue classes, pkgs;
365   int i, j, mode, l, linel;
366   char line[76];
367   const char *n;
368
369   queue_init(&classes);
370   queue_init(&pkgs);
371   mode = SOLVER_TRANSACTION_SHOW_OBSOLETES | SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE;
372   transaction_classify(trans, mode, &classes);
373   for (i = 0; i < classes.count; i += 4)
374     {
375       Id class = classes.elements[i];
376       Id cnt = classes.elements[i + 1];
377       switch(class)
378         {
379         case SOLVER_TRANSACTION_ERASE:
380           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d erased packages:\n", cnt);
381           break;
382         case SOLVER_TRANSACTION_INSTALL:
383           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d installed packages:\n", cnt);
384           break;
385         case SOLVER_TRANSACTION_REINSTALLED:
386           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d reinstalled packages:\n", cnt);
387           break;
388         case SOLVER_TRANSACTION_DOWNGRADED:
389           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d downgraded packages:\n", cnt);
390           break;
391         case SOLVER_TRANSACTION_CHANGED:
392           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d changed packages:\n", cnt);
393           break;
394         case SOLVER_TRANSACTION_UPGRADED:
395           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d upgraded packages:\n", cnt);
396           break;
397         case SOLVER_TRANSACTION_VENDORCHANGE:
398           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d vendor changes from '%s' to '%s':\n", cnt, id2strnone(pool, classes.elements[i + 2]), id2strnone(pool, classes.elements[i + 3]));
399           break;
400         case SOLVER_TRANSACTION_ARCHCHANGE:
401           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d arch changes from %s to %s:\n", cnt, pool_id2str(pool, classes.elements[i + 2]), pool_id2str(pool, classes.elements[i + 3]));
402           break;
403         default:
404           class = SOLVER_TRANSACTION_IGNORE;
405           break;
406         }
407       if (class == SOLVER_TRANSACTION_IGNORE)
408         continue;
409       transaction_classify_pkgs(trans, mode, class, classes.elements[i + 2], classes.elements[i + 3], &pkgs);
410       *line = 0;
411       linel = 0;
412       for (j = 0; j < pkgs.count; j++)
413         {
414           Id p = pkgs.elements[j];
415           Solvable *s = pool->solvables + p;
416           Solvable *s2;
417
418           switch(class)
419             {
420             case SOLVER_TRANSACTION_DOWNGRADED:
421             case SOLVER_TRANSACTION_UPGRADED:
422               s2 = pool->solvables + transaction_obs_pkg(trans, p);
423               POOL_DEBUG(SOLV_DEBUG_RESULT, "  - %s -> %s\n", pool_solvable2str(pool, s), pool_solvable2str(pool, s2));
424               break;
425             case SOLVER_TRANSACTION_VENDORCHANGE:
426             case SOLVER_TRANSACTION_ARCHCHANGE:
427               n = pool_id2str(pool, s->name);
428               l = strlen(n);
429               if (l + linel > sizeof(line) - 3)
430                 {
431                   if (*line)
432                     POOL_DEBUG(SOLV_DEBUG_RESULT, "    %s\n", line);
433                   *line = 0;
434                   linel = 0;
435                 }
436               if (l + linel > sizeof(line) - 3)
437                 POOL_DEBUG(SOLV_DEBUG_RESULT, "    %s\n", n);
438               else
439                 {
440                   if (*line)
441                     {
442                       strcpy(line + linel, ", ");
443                       linel += 2;
444                     }
445                   strcpy(line + linel, n);
446                   linel += l;
447                 }
448               break;
449             default:
450               POOL_DEBUG(SOLV_DEBUG_RESULT, "  - %s\n", pool_solvable2str(pool, s));
451               break;
452             }
453         }
454       if (*line)
455         POOL_DEBUG(SOLV_DEBUG_RESULT, "    %s\n", line);
456       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
457     }
458   queue_free(&classes);
459   queue_free(&pkgs);
460 }
461
462 void
463 solver_printproblemruleinfo(Solver *solv, Id probr)
464 {
465   Pool *pool = solv->pool;
466   Id dep, source, target;
467   SolverRuleinfo type = solver_ruleinfo(solv, probr, &source, &target, &dep);
468
469   POOL_DEBUG(SOLV_DEBUG_RESULT, "%s\n", solver_problemruleinfo2str(solv, type, source, target, dep));
470 }
471
472 void
473 solver_printprobleminfo(Solver *solv, Id problem)
474 {
475   solver_printproblemruleinfo(solv, solver_findproblemrule(solv, problem));
476 }
477
478 void
479 solver_printcompleteprobleminfo(Solver *solv, Id problem)
480 {
481   Queue q;
482   Id probr;
483   int i, nobad = 0;
484
485   queue_init(&q);
486   solver_findallproblemrules(solv, problem, &q);
487   for (i = 0; i < q.count; i++)
488     {
489       probr = q.elements[i];
490       if (!(probr >= solv->updaterules && probr < solv->updaterules_end) && !(probr >= solv->jobrules && probr < solv->jobrules_end))
491         {
492           nobad = 1;
493           break;
494         }
495     }
496   for (i = 0; i < q.count; i++)
497     {
498       probr = q.elements[i];
499       if (nobad && ((probr >= solv->updaterules && probr < solv->updaterules_end) || (probr >= solv->jobrules && probr < solv->jobrules_end)))
500         continue;
501       solver_printproblemruleinfo(solv, probr);
502     }
503   queue_free(&q);
504 }
505
506 static int illegals[] = {
507   POLICY_ILLEGAL_DOWNGRADE,
508   POLICY_ILLEGAL_NAMECHANGE,
509   POLICY_ILLEGAL_ARCHCHANGE,
510   POLICY_ILLEGAL_VENDORCHANGE,
511   0
512 };
513
514 void
515 solver_printsolution(Solver *solv, Id problem, Id solution)
516 {
517   Pool *pool = solv->pool;
518   Id p, rp, element;
519
520   element = 0;
521   while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
522     {
523       if (p > 0 && rp > 0)
524         {
525           /* for replacements we want to know why it was illegal */
526           Solvable *s = pool->solvables + p, *rs = pool->solvables + rp;
527           int illegal = policy_is_illegal(solv, s, rs, 0);
528           if (illegal)
529             {
530               int i;
531               for (i = 0; illegals[i]; i++)
532                 if ((illegal & illegals[i]) != 0)
533                   {
534                     POOL_DEBUG(SOLV_DEBUG_RESULT, "  - allow %s\n", policy_illegal2str(solv, illegals[i], s, rs));
535                     illegal ^= illegals[i];
536                   }
537               if (!illegal)
538                 continue;
539             }
540         }
541       POOL_DEBUG(SOLV_DEBUG_RESULT, "  - %s\n", solver_solutionelement2str(solv, p, rp));
542     }
543 }
544
545 void
546 solver_printallsolutions(Solver *solv)
547 {
548   Pool *pool = solv->pool;
549   int pcnt;
550   Id problem, solution;
551
552   POOL_DEBUG(SOLV_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
553   pcnt = 0;
554   problem = 0;
555   while ((problem = solver_next_problem(solv, problem)) != 0)
556     {
557       pcnt++;
558       POOL_DEBUG(SOLV_DEBUG_RESULT, "Problem %d:\n", pcnt);
559       POOL_DEBUG(SOLV_DEBUG_RESULT, "====================================\n");
560 #if 1
561       solver_printprobleminfo(solv, problem);
562 #else
563       solver_printcompleteprobleminfo(solv, problem);
564 #endif
565       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
566       solution = 0;
567       while ((solution = solver_next_solution(solv, problem, solution)) != 0)
568         {
569           solver_printsolution(solv, problem, solution);
570           POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
571         }
572     }
573 }
574