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