diff --git a/pkgs/lib/debug.nix b/pkgs/lib/debug.nix index 5d411b864ece..e0f8b414a0de 100644 --- a/pkgs/lib/debug.nix +++ b/pkgs/lib/debug.nix @@ -1,6 +1,9 @@ let lib = import ./default.nix; -inherit (builtins) trace attrNamesToStr isAttrs isFunction isList isInt isString head substring attrNames; +inherit (builtins) trace attrNamesToStr isAttrs isFunction isList isInt + isString isBool head substring attrNames; + +inherit (lib) all id mapAttrsFlatten; in @@ -21,7 +24,7 @@ rec { traceVal = if builtins ? trace then x: (builtins.trace x x) else x: x; traceXMLVal = if builtins ? trace then x: (builtins.trace (builtins.toXML x) x) else x: x; - + traceXMLValMarked = str: if builtins ? trace then x: (builtins.trace ( str + builtins.toXML x) x) else x: x; # this can help debug your code as well - designed to not produce thousands of lines traceShowVal = x : trace (showVal x) x; @@ -42,6 +45,7 @@ rec { else "x is probably a path `${substring 0 50 (toString x)}'"; # trace the arguments passed to function and its result + # maybe rewrite these functions in a traceCallXml like style. Then one function is enough traceCall = n : f : a : let t = n2 : x : traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a)); traceCall2 = n : f : a : b : let t = n2 : x : traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a) (t "arg 2" b)); traceCall3 = n : f : a : b : c : let t = n2 : x : traceShowValMarked "${n} ${n2}:" x; in t "result" (f (t "arg 1" a) (t "arg 2" b) (t "arg 3" c)); @@ -61,4 +65,47 @@ rec { then [ { inherit name; expected = test.expected; result = test.expr; } ] else [] ) tests)); + + + # evaluate everything once so that errors will occur earlier + # hacky: traverse attrs by adding a dummy + # ignores functions (should this behavior change?) See strictf + # + # Note: This should be a primop! Something like seq of haskell would be nice to + # have as well. It's used fore debugging only anyway + strict = x : + let + traverse = x : + if isString x then true + else if isAttrs x then + if x ? outPath then true + else all id (mapAttrsFlatten (n: traverse) x) + else if isList x then + all id (map traverse x) + else if isBool x then true + else if isFunction x then true + else if isInt x then true + else if x == null then true + else true; # a (store) path? + in if (traverse x) then x else throw "else never reached"; + + # example: (traceCallXml "myfun" id 3) will output something like + # calling myfun arg 1: 3 result: 3 + # this forces deep evaluation of all arguments and the result! + # note: if result doesn't evaluate you'll get no trace at all (FIXME) + # args should be printed in any case + traceCallXml = a: + if !isInt a then + traceCallXml 1 "calling ${a}\n" + else + let nr = a; + in (str: expr: + if isFunction expr then + (arg: + traceCallXml (builtins.add 1 nr) "${str}\n arg ${builtins.toString nr} is \n ${builtins.toXML (strict arg)}" (expr arg) + ) + else + let r = strict expr; + in builtins.trace "${str}\n result:\n${builtins.toXML r}" r + ); }