98_fhemdebug.pm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. ##############################################
  2. # $Id: 98_fhemdebug.pm 16769 2018-05-24 09:45:32Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. my $fhemdebug_enabled;
  7. my $main_callfn;
  8. sub
  9. fhemdebug_Initialize($){
  10. $cmds{"fhemdebug"}{Fn} = "fhemdebug_Fn";
  11. $cmds{"fhemdebug"}{Hlp} = "{start|stop|status}";
  12. }
  13. sub
  14. fhemdebug_Fn($$)
  15. {
  16. my ($cl,$param) = @_;
  17. if($param eq "enable") {
  18. return "fhemdebug is already enabled" if($fhemdebug_enabled);
  19. local $SIG{__WARN__} = sub { };
  20. $main_callfn = \&CallFn;
  21. *CallFn = \&fhemdebug_CallFn;
  22. $fhemdebug_enabled = 1;
  23. return undef;
  24. } elsif($param eq "disable") {
  25. return "fhemdebug is already disabled" if(!$fhemdebug_enabled);
  26. local $SIG{__WARN__} = sub { };
  27. *CallFn = $main_callfn;
  28. $fhemdebug_enabled = 0;
  29. return undef;
  30. } elsif($param eq "status") {
  31. return "fhemdebug is ".($fhemdebug_enabled ? "enabled":"disabled");
  32. } elsif($param =~ m/^memusage/) {
  33. return fhemdebug_memusage($param);
  34. } elsif($param =~ m/^timerList/) {
  35. return fhemdebug_timerList($param);
  36. } elsif($param =~ m/^addTimerStacktrace/) {
  37. $param =~ s/addTimerStacktrace\s*//;
  38. $addTimerStacktrace = $param;
  39. return;
  40. } else {
  41. return "Usage: fhemdebug {enable | disable | status | memusage | ".
  42. "timerList | addTimerStacktrace {0|1} }";
  43. }
  44. }
  45. sub
  46. fhemdebug_CheckDefs($@)
  47. {
  48. my ($txt, $dev, $n) = @_;
  49. foreach my $d (keys %defs) {
  50. if(!defined($d)) {
  51. Log 1, "ERROR: undef \$defs entry found ($txt $dev $n)";
  52. delete($defs{undef});
  53. next;
  54. }
  55. if($d eq "") {
  56. Log 1, "ERROR: '' \$defs entry found ($txt $dev $n)";
  57. delete($defs{''});
  58. next;
  59. }
  60. if(ref $defs{$d} ne "HASH") {
  61. Log 1, "ERROR: \$defs{$d} is not a HASH ($txt $dev $n)";
  62. delete($defs{$d});
  63. next;
  64. }
  65. if(!$defs{$d}{TYPE}) {
  66. Log 1, "ERROR: >$d< has no TYPE, but following keys: >".
  67. join(",", sort keys %{$defs{$d}})."<".
  68. "($txt $dev $n)";
  69. delete($defs{$d});
  70. next;
  71. }
  72. }
  73. }
  74. sub
  75. fhemdebug_CallFn(@)
  76. {
  77. #Log 1, "fhemdebug_CallFn $_[0] $_[1];
  78. if(wantarray) {
  79. fhemdebug_CheckDefs("before", @_);
  80. no strict "refs";
  81. my @ret = &{$main_callfn}(@_);
  82. use strict "refs";
  83. fhemdebug_CheckDefs("after", @_);
  84. return @ret;
  85. } else {
  86. fhemdebug_CheckDefs("before", @_);
  87. no strict "refs";
  88. my $ret = &{$main_callfn}(@_);
  89. fhemdebug_CheckDefs("after", @_);
  90. use strict "refs";
  91. return $ret;
  92. }
  93. }
  94. sub
  95. fhemdebug_memusage($)
  96. {
  97. my ($param) = @_;
  98. eval "use Devel::Size";
  99. return $@ if($@);
  100. $Devel::Size::warn = 0;
  101. my @param = split(" ", $param);
  102. my $max = 50;
  103. my $re;
  104. $max = pop(@param) if(@param > 1 && $param[$#param] =~ m/^\d+$/);
  105. $re = pop(@param) if(@param > 1);
  106. my %ts;
  107. my %mh = (defs=>1, modules=>1, selectlist=>1, attr=>1, readyfnlist=>1);
  108. my $collectSize = sub($$$$)
  109. {
  110. my ($fn, $h, $mname,$cleanUp) = @_;
  111. return 0 if($h->{__IN__CS__}); # save us from endless recursion
  112. return 0 if($h->{__IN__CSS__} && !$cleanUp);
  113. $h->{__IN__CSS__} = 1 if(!$cleanUp);
  114. $h->{__IN__CS__} = 1;
  115. my $sum = 0;
  116. foreach my $n (sort keys %$h) {
  117. next if(!$n || $n =~ m/^[^A-Za-z]$/ || $n eq "__IN__CS__");
  118. my $ref = ref $h->{$n};
  119. my $name = ($mname eq "main::" ? "$mname$n" : "${mname}::$n");
  120. $ref = "HASH" if(!$ref && $mname eq "main::" && $mh{$n});
  121. next if($n eq "main::" || $n eq "IODev" ||
  122. $ref eq "CODE" || main->can($name) || $ref =~ m/::/);
  123. Log 5, " Check $name / $mname / $n / $ref"; # Crash-debugging
  124. if($ref eq "HASH") {
  125. next if($mname ne "main::defs" && $h->{$n}{TYPE} && $h->{$n}{NAME});
  126. $sum += $fn->($fn, $h->{$n}, $name, $cleanUp);
  127. } else {
  128. my $sz = Devel::Size::size($h->{$n});
  129. $ts{$name} = $sz if(!$cleanUp);
  130. $sum += $sz;
  131. }
  132. }
  133. delete($h->{__IN__CS__});
  134. delete($h->{__IN__CSS__}) if($cleanUp);
  135. $sum += Devel::Size::size($h);
  136. $ts{$mname} = $sum if($mname ne "main::" && !$cleanUp);
  137. return $sum;
  138. };
  139. $collectSize->($collectSize, \%main::, "main::", 0);
  140. $collectSize->($collectSize, \%main::, "main::", 1);
  141. my @sts = sort { $ts{$b} <=> $ts{$a} } keys %ts;
  142. my @ret;
  143. for(my $i=0; $i < @sts; $i++) {
  144. next if($re && $sts[$i] !~ m/$re/);
  145. push @ret, sprintf("%4d. %-30s %8d", $i+1,substr($sts[$i],6),$ts{$sts[$i]});
  146. last if(@ret >= $max);
  147. }
  148. return join("\n", @ret);
  149. }
  150. sub
  151. fhemdebug_timerList($)
  152. {
  153. my ($param) = @_;
  154. my @res;
  155. for my $h (@intAtA) {
  156. my $tt = $h->{TRIGGERTIME};
  157. push(@res, sprintf("%s.%05d %s%s",
  158. FmtDateTime($tt), int(($tt-int($tt))*100000), $h->{FN},
  159. $h->{STACKTRACE} ? $h->{STACKTRACE} : ""));
  160. }
  161. return join("\n", @res);
  162. }
  163. 1;
  164. =pod
  165. =item command
  166. =item summary try to localize FHEM error messages
  167. =item summary_DE Hilfe bei der Lokalisierung von Fehlermeldungen
  168. =begin html
  169. <a name="fhemdebug"></a>
  170. <h3>fhemdebug</h3>
  171. <ul>
  172. <code>fhemdebug &lt;command&gt;</code><br>
  173. <br>
  174. where &lt;command&gt; is one of
  175. <ul>
  176. <li>enable/disable/status<br>
  177. fhemdebug produces debug information in the FHEM Log to help localize
  178. certain error messages. Currently following errors are examined:
  179. <ul>
  180. - Error: &gt;...&lt; has no TYPE, but following keys: &gt;...&lt;<br>
  181. </ul>
  182. As it frequently examines internal data-structures, it uses a lot of CPU,
  183. it is not recommended to enable it all the time. A FHEM restart after
  184. disabling it is not necessary.<br>
  185. </li>
  186. <li>memusage [regexp] [nr]<br>
  187. Dump the name of the first nr datastructures with the largest memory
  188. footprint. Filter the names by regexp, if specified.<br>
  189. <b>Notes</b>:
  190. <ul>
  191. <li>this function depends on the Devel::Size module, so this must be
  192. installed first.</li>
  193. <li>The used function Devel::Size::size may crash perl (and FHEM) for
  194. functions and some other data structures. memusage tries to avoid to
  195. call it for such data structures, but as the problem is not
  196. identified, it may crash your currently running instance. It works
  197. for me, but make sure you saved your fhem.cfg before calling it.</li>
  198. <li>To avoid the crash, the size of same data is not computed, so the
  199. size reported is probably inaccurate, it should only be used as a
  200. hint. </li>
  201. </ul>
  202. </li>
  203. <li>timerList<br>
  204. show the list of InternalTimer calls.
  205. </li>
  206. <li>addTimerStacktrace {1|0}<br>
  207. enable or disable the registering the stacktrace of each InternalTimer
  208. call. This stacktrace will be shown in the timerList command.
  209. </li>
  210. </ul>
  211. </ul>
  212. =end html
  213. =cut