Imported Upstream version 0.6.7
[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 (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   solver_printrule(solv, type, r);
132 }
133
134 void
135 solver_printproblem(Solver *solv, Id v)
136 {
137   Pool *pool = solv->pool;
138   int i;
139   Rule *r;
140   Id *jp;
141
142   if (v > 0)
143     solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, solv->rules + v);
144   else
145     {
146       v = -(v + 1);
147       POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "JOB %d\n", v);
148       jp = solv->ruletojob.elements;
149       for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
150         if (*jp == v)
151           {
152             POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "- ");
153             solver_printrule(solv, SOLV_DEBUG_SOLUTIONS, r);
154           }
155       POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "ENDJOB\n");
156     }
157 }
158
159 void
160 solver_printwatches(Solver *solv, int type)
161 {
162   Pool *pool = solv->pool;
163   int counter;
164
165   POOL_DEBUG(type, "Watches: \n");
166   for (counter = -(pool->nsolvables - 1); counter < pool->nsolvables; counter++)
167     POOL_DEBUG(type, "    solvable [%d] -- rule [%d]\n", counter, solv->watches[counter + pool->nsolvables]);
168 }
169
170 void
171 solver_printdecisionq(Solver *solv, int type)
172 {
173   Pool *pool = solv->pool;
174   int i;
175   Id p, why;
176
177   POOL_DEBUG(type, "Decisions:\n");
178   for (i = 0; i < solv->decisionq.count; i++)
179     {
180       p = solv->decisionq.elements[i];
181       if (p > 0)
182         POOL_DEBUG(type, "%d %d install  %s, ", i, solv->decisionmap[p], pool_solvid2str(pool, p));
183       else
184         POOL_DEBUG(type, "%d %d conflict %s, ", i, -solv->decisionmap[-p], pool_solvid2str(pool, -p));
185       why = solv->decisionq_why.elements[i];
186       if (why > 0)
187         {
188           POOL_DEBUG(type, "forced by ");
189           solver_printruleclass(solv, type, solv->rules + why);
190         }
191       else if (why < 0)
192         {
193           POOL_DEBUG(type, "chosen from ");
194           solver_printruleclass(solv, type, solv->rules - why);
195         }
196       else
197         POOL_DEBUG(type, "picked for some unknown reason.\n");
198     }
199 }
200
201 /*
202  * printdecisions
203  */
204
205 void
206 solver_printdecisions(Solver *solv)
207 {
208   Pool *pool = solv->pool;
209   Repo *installed = solv->installed;
210   Transaction *trans = solver_create_transaction(solv);
211   Id p, type;
212   int i, j;
213   Solvable *s;
214   Queue iq;
215   Queue recommendations;
216   Queue suggestions;
217   Queue orphaned;
218
219   POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
220   POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction:\n");
221
222   queue_init(&iq);
223   for (i = 0; i < trans->steps.count; i++)
224     {
225       p = trans->steps.elements[i];
226       s = pool->solvables + p;
227       type = transaction_type(trans, p, SOLVER_TRANSACTION_SHOW_ACTIVE|SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES|SOLVER_TRANSACTION_SHOW_MULTIINSTALL);
228       switch(type)
229         {
230         case SOLVER_TRANSACTION_MULTIINSTALL:
231           POOL_DEBUG(SOLV_DEBUG_RESULT, "  multi install %s", pool_solvable2str(pool, s));
232           break;
233         case SOLVER_TRANSACTION_MULTIREINSTALL:
234           POOL_DEBUG(SOLV_DEBUG_RESULT, "  multi reinstall %s", pool_solvable2str(pool, s));
235           break;
236         case SOLVER_TRANSACTION_INSTALL:
237           POOL_DEBUG(SOLV_DEBUG_RESULT, "  install   %s", pool_solvable2str(pool, s));
238           break;
239         case SOLVER_TRANSACTION_REINSTALL:
240           POOL_DEBUG(SOLV_DEBUG_RESULT, "  reinstall %s", pool_solvable2str(pool, s));
241           break;
242         case SOLVER_TRANSACTION_DOWNGRADE:
243           POOL_DEBUG(SOLV_DEBUG_RESULT, "  downgrade %s", pool_solvable2str(pool, s));
244           break;
245         case SOLVER_TRANSACTION_CHANGE:
246           POOL_DEBUG(SOLV_DEBUG_RESULT, "  change    %s", pool_solvable2str(pool, s));
247           break;
248         case SOLVER_TRANSACTION_UPGRADE:
249         case SOLVER_TRANSACTION_OBSOLETES:
250           POOL_DEBUG(SOLV_DEBUG_RESULT, "  upgrade   %s", pool_solvable2str(pool, s));
251           break;
252         case SOLVER_TRANSACTION_ERASE:
253           POOL_DEBUG(SOLV_DEBUG_RESULT, "  erase     %s", pool_solvable2str(pool, s));
254           break;
255         default:
256           break;
257         }
258       switch(type)
259         {
260         case SOLVER_TRANSACTION_INSTALL:
261         case SOLVER_TRANSACTION_ERASE:
262         case SOLVER_TRANSACTION_MULTIINSTALL:
263         case SOLVER_TRANSACTION_MULTIREINSTALL:
264           POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
265           break;
266         case SOLVER_TRANSACTION_REINSTALL:
267         case SOLVER_TRANSACTION_DOWNGRADE:
268         case SOLVER_TRANSACTION_CHANGE:
269         case SOLVER_TRANSACTION_UPGRADE:
270         case SOLVER_TRANSACTION_OBSOLETES:
271           transaction_all_obs_pkgs(trans, p, &iq);
272           if (iq.count)
273             {
274               POOL_DEBUG(SOLV_DEBUG_RESULT, "  (obsoletes");
275               for (j = 0; j < iq.count; j++)
276                 POOL_DEBUG(SOLV_DEBUG_RESULT, " %s", pool_solvid2str(pool, iq.elements[j]));
277               POOL_DEBUG(SOLV_DEBUG_RESULT, ")");
278             }
279           POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
280           break;
281         default:
282           break;
283         }
284     }
285   queue_free(&iq);
286
287   POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
288
289   queue_init(&recommendations);
290   queue_init(&suggestions);
291   queue_init(&orphaned);
292   solver_get_recommendations(solv, &recommendations, &suggestions, 0);
293   solver_get_orphaned(solv, &orphaned);
294   if (recommendations.count)
295     {
296       POOL_DEBUG(SOLV_DEBUG_RESULT, "recommended packages:\n");
297       for (i = 0; i < recommendations.count; i++)
298         {
299           s = pool->solvables + recommendations.elements[i];
300           if (solv->decisionmap[recommendations.elements[i]] > 0)
301             {
302               if (installed && s->repo == installed)
303                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (installed)\n", pool_solvable2str(pool, s));
304               else
305                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (selected)\n", pool_solvable2str(pool, s));
306             }
307           else
308             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s\n", pool_solvable2str(pool, s));
309         }
310       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
311     }
312
313   if (suggestions.count)
314     {
315       POOL_DEBUG(SOLV_DEBUG_RESULT, "suggested packages:\n");
316       for (i = 0; i < suggestions.count; i++)
317         {
318           s = pool->solvables + suggestions.elements[i];
319           if (solv->decisionmap[suggestions.elements[i]] > 0)
320             {
321               if (installed && s->repo == installed)
322                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (installed)\n", pool_solvable2str(pool, s));
323               else
324                 POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (selected)\n", pool_solvable2str(pool, s));
325             }
326           else
327             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s\n", pool_solvable2str(pool, s));
328         }
329       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
330     }
331   if (orphaned.count)
332     {
333       POOL_DEBUG(SOLV_DEBUG_RESULT, "orphaned packages:\n");
334       for (i = 0; i < orphaned.count; i++)
335         {
336           s = pool->solvables + orphaned.elements[i];
337           if (solv->decisionmap[solv->orphaned.elements[i]] > 0)
338             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (kept)\n", pool_solvable2str(pool, s));
339           else
340             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s (erased)\n", pool_solvable2str(pool, s));
341         }
342       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
343     }
344   queue_free(&recommendations);
345   queue_free(&suggestions);
346   queue_free(&orphaned);
347   transaction_free(trans);
348 }
349
350 static inline
351 const char *id2strnone(Pool *pool, Id id)
352 {
353   return !id || id == 1 ? "(none)" : pool_id2str(pool, id);
354 }
355
356 void
357 transaction_print(Transaction *trans)
358 {
359   Pool *pool = trans->pool;
360   Queue classes, pkgs;
361   int i, j, mode, l, linel;
362   char line[76];
363   const char *n;
364
365   queue_init(&classes);
366   queue_init(&pkgs);
367   mode = SOLVER_TRANSACTION_SHOW_OBSOLETES | SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE;
368   transaction_classify(trans, mode, &classes);
369   for (i = 0; i < classes.count; i += 4)
370     {
371       Id class = classes.elements[i];
372       Id cnt = classes.elements[i + 1];
373       switch(class)
374         {
375         case SOLVER_TRANSACTION_ERASE:
376           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d erased packages:\n", cnt);
377           break;
378         case SOLVER_TRANSACTION_INSTALL:
379           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d installed packages:\n", cnt);
380           break;
381         case SOLVER_TRANSACTION_REINSTALLED:
382           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d reinstalled packages:\n", cnt);
383           break;
384         case SOLVER_TRANSACTION_DOWNGRADED:
385           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d downgraded packages:\n", cnt);
386           break;
387         case SOLVER_TRANSACTION_CHANGED:
388           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d changed packages:\n", cnt);
389           break;
390         case SOLVER_TRANSACTION_UPGRADED:
391           POOL_DEBUG(SOLV_DEBUG_RESULT, "%d upgraded packages:\n", cnt);
392           break;
393         case SOLVER_TRANSACTION_VENDORCHANGE:
394           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]));
395           break;
396         case SOLVER_TRANSACTION_ARCHCHANGE:
397           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]));
398           break;
399         default:
400           class = SOLVER_TRANSACTION_IGNORE;
401           break;
402         }
403       if (class == SOLVER_TRANSACTION_IGNORE)
404         continue;
405       transaction_classify_pkgs(trans, mode, class, classes.elements[i + 2], classes.elements[i + 3], &pkgs);
406       *line = 0;
407       linel = 0;
408       for (j = 0; j < pkgs.count; j++)
409         {
410           Id p = pkgs.elements[j];
411           Solvable *s = pool->solvables + p;
412           Solvable *s2;
413
414           switch(class)
415             {
416             case SOLVER_TRANSACTION_DOWNGRADED:
417             case SOLVER_TRANSACTION_UPGRADED:
418               s2 = pool->solvables + transaction_obs_pkg(trans, p);
419               POOL_DEBUG(SOLV_DEBUG_RESULT, "  - %s -> %s\n", pool_solvable2str(pool, s), pool_solvable2str(pool, s2));
420               break;
421             case SOLVER_TRANSACTION_VENDORCHANGE:
422             case SOLVER_TRANSACTION_ARCHCHANGE:
423               n = pool_id2str(pool, s->name);
424               l = strlen(n);
425               if (l + linel > sizeof(line) - 3)
426                 {
427                   if (*line)
428                     POOL_DEBUG(SOLV_DEBUG_RESULT, "    %s\n", line);
429                   *line = 0;
430                   linel = 0;
431                 }
432               if (l + linel > sizeof(line) - 3)
433                 POOL_DEBUG(SOLV_DEBUG_RESULT, "    %s\n", n);
434               else
435                 {
436                   if (*line)
437                     {
438                       strcpy(line + linel, ", ");
439                       linel += 2;
440                     }
441                   strcpy(line + linel, n);
442                   linel += l;
443                 }
444               break;
445             default:
446               POOL_DEBUG(SOLV_DEBUG_RESULT, "  - %s\n", pool_solvable2str(pool, s));
447               break;
448             }
449         }
450       if (*line)
451         POOL_DEBUG(SOLV_DEBUG_RESULT, "    %s\n", line);
452       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
453     }
454   queue_free(&classes);
455   queue_free(&pkgs);
456 }
457
458 void
459 solver_printproblemruleinfo(Solver *solv, Id probr)
460 {
461   Pool *pool = solv->pool;
462   Id dep, source, target;
463   SolverRuleinfo type = solver_ruleinfo(solv, probr, &source, &target, &dep);
464
465   POOL_DEBUG(SOLV_DEBUG_RESULT, "%s\n", solver_problemruleinfo2str(solv, type, source, target, dep));
466 }
467
468 void
469 solver_printprobleminfo(Solver *solv, Id problem)
470 {
471   solver_printproblemruleinfo(solv, solver_findproblemrule(solv, problem));
472 }
473
474 void
475 solver_printcompleteprobleminfo(Solver *solv, Id problem)
476 {
477   Queue q;
478   Id probr;
479   int i, nobad = 0;
480
481   queue_init(&q);
482   solver_findallproblemrules(solv, problem, &q);
483   for (i = 0; i < q.count; i++)
484     {
485       probr = q.elements[i];
486       if (!(probr >= solv->updaterules && probr < solv->updaterules_end) && !(probr >= solv->jobrules && probr < solv->jobrules_end))
487         {
488           nobad = 1;
489           break;
490         }
491     }
492   for (i = 0; i < q.count; i++)
493     {
494       probr = q.elements[i];
495       if (nobad && ((probr >= solv->updaterules && probr < solv->updaterules_end) || (probr >= solv->jobrules && probr < solv->jobrules_end)))
496         continue;
497       solver_printproblemruleinfo(solv, probr);
498     }
499   queue_free(&q);
500 }
501
502 static int illegals[] = {
503   POLICY_ILLEGAL_DOWNGRADE,
504   POLICY_ILLEGAL_NAMECHANGE,
505   POLICY_ILLEGAL_ARCHCHANGE,
506   POLICY_ILLEGAL_VENDORCHANGE,
507   0
508 };
509
510 void
511 solver_printsolution(Solver *solv, Id problem, Id solution)
512 {
513   Pool *pool = solv->pool;
514   Id p, rp, element;
515
516   element = 0;
517   while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
518     {
519       if (p > 0 && rp > 0)
520         {
521           /* for replacements we want to know why it was illegal */
522           Solvable *s = pool->solvables + p, *rs = pool->solvables + rp;
523           int illegal = policy_is_illegal(solv, s, rs, 0);
524           if (illegal)
525             {
526               int i;
527               for (i = 0; illegals[i]; i++)
528                 if ((illegal & illegals[i]) != 0)
529                   {
530                     POOL_DEBUG(SOLV_DEBUG_RESULT, "  - allow %s\n", policy_illegal2str(solv, illegals[i], s, rs));
531                     illegal ^= illegals[i];
532                   }
533               if (!illegal)
534                 continue;
535             }
536         }
537       POOL_DEBUG(SOLV_DEBUG_RESULT, "  - %s\n", solver_solutionelement2str(solv, p, rp));
538     }
539 }
540
541 void
542 solver_printallsolutions(Solver *solv)
543 {
544   Pool *pool = solv->pool;
545   int pcnt;
546   Id problem, solution;
547
548   POOL_DEBUG(SOLV_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
549   pcnt = 0;
550   problem = 0;
551   while ((problem = solver_next_problem(solv, problem)) != 0)
552     {
553       pcnt++;
554       POOL_DEBUG(SOLV_DEBUG_RESULT, "Problem %d:\n", pcnt);
555       POOL_DEBUG(SOLV_DEBUG_RESULT, "====================================\n");
556 #if 1
557       solver_printprobleminfo(solv, problem);
558 #else
559       solver_printcompleteprobleminfo(solv, problem);
560 #endif
561       POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
562       solution = 0;
563       while ((solution = solver_next_solution(solv, problem, solution)) != 0)
564         {
565           solver_printsolution(solv, problem, solution);
566           POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
567         }
568     }
569 }
570
571 void
572 solver_printtrivial(Solver *solv)
573 {
574   Pool *pool = solv->pool;
575   Queue in, out;
576   Id p;
577   const char *n;
578   Solvable *s;
579   int i;
580
581   queue_init(&in);
582   for (p = 1, s = pool->solvables + p; p < solv->pool->nsolvables; p++, s++)
583     {
584       n = pool_id2str(pool, s->name);
585       if (strncmp(n, "patch:", 6) != 0 && strncmp(n, "pattern:", 8) != 0)
586         continue;
587       queue_push(&in, p);
588     }
589   if (!in.count)
590     {
591       queue_free(&in);
592       return;
593     }
594   queue_init(&out);
595   solver_trivial_installable(solv, &in, &out);
596   POOL_DEBUG(SOLV_DEBUG_RESULT, "trivial installable status:\n");
597   for (i = 0; i < in.count; i++)
598     POOL_DEBUG(SOLV_DEBUG_RESULT, "  %s: %d\n", pool_solvid2str(pool, in.elements[i]), out.elements[i]);
599   POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");
600   queue_free(&in);
601   queue_free(&out);
602 }
603