Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.pl 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IPC::Open3;
  5. use Symbol 'gensym';
  6. use IO::Handle;
  7. use Test::More qw/ no_plan /;
  8. my $USDT_ARG_MAX = 32;
  9. if ($^O eq 'freebsd') {
  10. # FreeBSD currently only supports 5 arguments to USDT probes
  11. $USDT_ARG_MAX = 5;
  12. }
  13. my $arch;
  14. if (scalar @ARGV == 1) {
  15. $arch = $ARGV[0];
  16. }
  17. my $user_t = ($^O eq 'darwin') ? 'user_addr_t' : 'uintptr_t';
  18. run_tests('c', 'A');
  19. run_tests('i', 1);
  20. sub run_tests {
  21. my ($type, $start_arg) = @_;
  22. for my $i (0..$USDT_ARG_MAX) {
  23. my ($t_status, $d_status, $output) = run_dtrace('type'.$type, $i.'arg', split(//, $type x $i));
  24. is($t_status, 0, 'test exit status is 0');
  25. is($d_status, 0, 'dtrace exit status is 0');
  26. like($output, qr/type[ic]:\d+arg/, 'function and name match');
  27. my $arg = $start_arg;
  28. for my $j (0..$i - 1) {
  29. like($output, qr/arg$j:'\Q$arg\E'/, "type '$type' arg $j is $arg");
  30. if ($type eq 'i') {
  31. $arg++;
  32. }
  33. else {
  34. $arg = chr(ord($arg) + 1);
  35. }
  36. }
  37. }
  38. }
  39. # --------------------------------------------------------------------------
  40. sub gen_d {
  41. my (@types) = @_;
  42. my $d = 'testlibusdt*:::{ ';
  43. my $i = 0;
  44. for my $type (@types) {
  45. if ($type eq 'i') {
  46. $d .= "printf(\"arg$i:'%i' \", args[$i]); ";
  47. }
  48. if ($type eq 'c') {
  49. $d .= "printf(\"arg$i:'%s' \", copyinstr(($user_t)args[$i])); ";
  50. }
  51. $i++;
  52. }
  53. $d .= '}';
  54. return $d;
  55. }
  56. sub run_dtrace {
  57. my ($func, $name, @types) = @_;
  58. my $d = gen_d(@types);
  59. my @t_cmd;
  60. if (defined $arch) {
  61. @t_cmd = ("./test_usdt$arch", $func, $name, @types);
  62. }
  63. else {
  64. @t_cmd = ("./test_usdt", $func, $name, @types);
  65. }
  66. my ($d_wtr, $d_rdr, $d_err);
  67. my ($t_wtr, $t_rdr, $t_err);
  68. $d_err = gensym;
  69. $t_err = gensym;
  70. #diag(join(' ', @t_cmd));
  71. my $t_pid = open3($t_wtr, $t_rdr, $t_err, @t_cmd);
  72. my $enabled = $t_rdr->getline;
  73. my @d_cmd = ('/usr/sbin/dtrace', '-p', $t_pid, '-n', $d);
  74. #diag(join(' ', @d_cmd));
  75. my $d_pid = open3($d_wtr, $d_rdr, $d_err, @d_cmd);
  76. my $matched = $d_err->getline; # expect "matched 1 probe"
  77. $t_wtr->print("go\n");
  78. $t_wtr->flush;
  79. waitpid( $t_pid, 0 );
  80. my $t_status = $? >> 8;
  81. my ($header, $output) = ($d_rdr->getline, $d_rdr->getline);
  82. chomp $header;
  83. chomp $output;
  84. #diag("DTrace header: $header\n");
  85. #diag("DTrace output: $output\n");
  86. waitpid( $d_pid, 0 );
  87. my $d_status = $? >> 8;
  88. while (!$d_err->eof) {
  89. my $error = $d_err->getline;
  90. chomp $error;
  91. #diag "DTrace error: $error";
  92. }
  93. return ($t_status, $d_status, $output || '');
  94. }