home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR41 / CTASK22D.ZIP / TSKSNAP.C < prev    next >
C/C++ Source or Header  |  1993-06-08  |  12KB  |  399 lines

  1. /*
  2.    --- Version 2.2 93-06-08 10:26 ---
  3.  
  4.    TSKSNAP.C - CTask State Snaphot
  5.  
  6.    Public Domain Software written by
  7.       Thomas Wagner
  8.       Ferrari electronic Gmbh
  9.       Beusselstrasse 27
  10.       D-1000 Berlin 21
  11.       Germany
  12.  
  13.    This file is new with Version 1.1
  14.    It has been changed to use the assembler printf routines in 
  15.    version 2.1.
  16. */
  17.  
  18. #include "tsk.h"
  19. #include "tsklocal.h"
  20. #include "tsksup.h"
  21. #include "tskprf.h"
  22. #include "tskdeb.h"
  23.  
  24. #include <stdarg.h>
  25.  
  26. #define DBPSP  0  /* If defined nonzero, the PSP info is displayed in place
  27.                      of stack-pointer and stack-bottom in dump_tasks. */
  28.  
  29. #define NOSTDIO 0  /* If defined nonzero, the routines using the standard
  30.                       I/O routines are not included in the compilation. */
  31.  
  32. #if (DOS)
  33.  
  34. local char *sn_state [] = { "*???*", "Stop", "Delay", "Wait", "Elig", "Run" };
  35. local char *sn_kind [] = { "*Undef*", "Task", "Flag", "Resource", "Counter",
  36.                            "Mailbox", "Pipe", "Wpipe", "Buffer" };
  37.  
  38. local int maxrow, row;
  39.  
  40. #if (CHECKING)
  41. local word Staticfunc stack_free (tcbptr task)
  42. {
  43.    word i;
  44.    byteptr ptr;
  45.  
  46.    ptr = task->stkbot;
  47.    for (i = 0; *ptr++ == (byte)i; i++)
  48.       ;
  49.    return i;
  50. }
  51. #endif
  52.  
  53. local int CStaticfunc xprintf (int handle, byteptr format, ...)
  54. {
  55.    va_list argptr;
  56.  
  57.    if (maxrow > 0 && row >= maxrow)
  58.       return -1;
  59.  
  60.    va_start (argptr, format);
  61.  
  62.    if (handle >= 0)
  63.       tsk_vfprintf (handle, format, argptr);
  64.    else if (handle == -1)
  65.       tsk_vprintf (format, argptr);
  66.    else if (handle == -3)
  67.       tsk_vcprintf (format, argptr);
  68.    else
  69.       tsk_vrprintf (format, argptr);
  70.  
  71.    va_end (argptr);
  72.    return 0;
  73. }
  74.  
  75.  
  76. local int Staticfunc sn_wlist (int f, byteptr str, queheadptr list, int col)
  77. {
  78.    queptr curr;
  79.  
  80.    curr = list->first;
  81.    if (curr->kind & Q_HEAD)
  82.       return col;
  83.    if (col)
  84.       {
  85.       row++;
  86.       xprintf (f, "\n%31c", ' ');
  87.       }
  88.    col = 52;
  89.    xprintf (f, "%6Fs(%Fp):  ", (farptr)str, list);
  90.    while (!(curr->kind & Q_HEAD))
  91.       {
  92.       if (col >= 60)
  93.          {
  94.          row++;
  95.          xprintf (f, "\n%51c", ' ');
  96.          col = 52;
  97.          }
  98.       xprintf (f, "%-8Fs(%Fp) ", ((tcbptr)curr)->name.name, curr);
  99.       col += 20;
  100.       curr = curr->next;
  101.       }
  102.    return col;
  103. }
  104.  
  105.  
  106. local void Staticfunc dgroup (int f, gcbptr group)
  107. {
  108.    xprintf (f, ">> Group %-8Fs (%Fp), C:%-8Fs (%Fp), B:%Fp, L:%Fp\n",
  109.             group->namelist.name, group, 
  110.             group->creator->name.name, group->creator,
  111.             group->branch, group->level);
  112.    row++;
  113. }
  114.  
  115.  
  116. local void Staticfunc event_group (int f, gcbptr base)
  117. {
  118.    queptr curr;
  119.    int col;
  120.    flagptr fl;
  121.    counterptr ct;
  122.    resourceptr rs;
  123.    mailboxptr mb;
  124.    pipeptr pp;
  125.    wpipeptr wp;
  126.    bufferptr bp;
  127.    gcbptr group;
  128.  
  129.    for (group = base; group != LNULL; group = group->level)
  130.       {
  131.       dgroup (f, group);
  132.       for (curr = group->namelist.list.first; !(curr->kind & Q_HEAD); curr = curr->next)
  133.          if (curr->kind != TYP_TCB)
  134.             {
  135.             if (xprintf (f, "%-8Fs   %-8Fs  ", ((nameptr)curr)->name,
  136.                          (farptr)sn_kind [curr->kind]))
  137.                return;
  138.             switch (curr->kind)
  139.                {
  140.                case TYP_FLAG: fl = (flagptr) ((nameptr)curr)->strucp;
  141.                               xprintf (f, "%8Fs  ", (farptr)((fl->state) ? "Set" : "Clear"));
  142.                               col = sn_wlist (f, "Set", &fl->wait_set, 0);
  143.                               sn_wlist (f, "Clear", &fl->wait_clear, col);
  144.                               break;
  145.  
  146.                case TYP_COUNTER: ct = (counterptr) ((nameptr)curr)->strucp;
  147.                               xprintf (f, "%8ld  ", ct->state);
  148.                               col = sn_wlist (f, "Set", &ct->wait_set, 0);
  149.                               sn_wlist (f, "Clear", &ct->wait_clear, col);
  150.                               break;
  151.  
  152.                case TYP_RESOURCE: rs = (resourceptr) ((nameptr)curr)->strucp;
  153.                               if (rs->count)
  154.                                  {
  155.                                  xprintf (f, "%8u  ", rs->count);
  156.                                  xprintf (f, "Owner (%Fp):  ", (farptr)&rs->owner);
  157.                                  xprintf (f, "%-8Fs(%Fp) ", rs->owner->name.name, 
  158.                                                          rs->owner);
  159.                                  col = 70;
  160.                                  }
  161.                               else
  162.                                  {
  163.                                  xprintf (f, "    Free  ");
  164.                                  col = 0;
  165.                                  }
  166.                               sn_wlist (f, "Wait", &rs->waiting, col);
  167.                               break;
  168.  
  169.                case TYP_MAILBOX: mb = (mailboxptr) ((nameptr)curr)->strucp;
  170.                               xprintf (f, "%8Fs  ", (farptr)((mb->mail_first == LNULL)
  171.                                        ? "Empty" : "Mail"));
  172.                               sn_wlist (f, "Mail", &mb->waiting, 0);
  173.                               break;
  174.  
  175.                case TYP_PIPE: pp = (pipeptr) ((nameptr)curr)->strucp;
  176.                               xprintf (f, "%8u  ", pp->filled);
  177.                               col = sn_wlist (f, "Read", &pp->wait_read, 0);
  178.                               col = sn_wlist (f, "Write", &pp->wait_write, col);
  179.                               sn_wlist (f, "Clear", &pp->wait_clear, col);
  180.                               break;
  181.  
  182.                case TYP_WPIPE: wp = (wpipeptr) ((nameptr)curr)->strucp;
  183.                               xprintf (f, "%8u  ", wp->filled);
  184.                               col = sn_wlist (f, "Read", &wp->wait_read, 0);
  185.                               col = sn_wlist (f, "Write", &wp->wait_write, col);
  186.                               sn_wlist (f, "Clear", &wp->wait_clear, col);
  187.                               break;
  188.  
  189.                case TYP_BUFFER: bp = (bufferptr) ((nameptr)curr)->strucp;
  190.                               xprintf (f, "%8u  ", bp->msgcnt);
  191.                               col = sn_wlist (f, "Read", &bp->buf_read.waiting, 0);
  192.                               sn_wlist (f, "Write", &bp->buf_write.waiting, col);
  193.                               break;
  194.  
  195.                default:       xprintf (f, "*Invalid Type %02x", curr->kind);
  196.                               break;
  197.                }
  198.             xprintf (f, "\n");
  199.             row++;
  200.             }
  201.       if (group->branch != LNULL)
  202.          event_group (f, group->branch);
  203.       }
  204. }
  205.  
  206.  
  207. local void Staticfunc dump_events (int f)
  208. {
  209.    if (xprintf (f, "List of Events:\n"))
  210.       return;
  211.    /*           1234567890123456789012345678901234567890123456789012345
  212.                 12345678   12345678  12345678  123456(123456789):        */
  213.    row++;
  214.    if (xprintf (f, "Name       Type         State     Waitfor(Queue)   Tasks\n"))
  215.       return;
  216.    row++;
  217.  
  218.    event_group (f, &GLOBDATA group);
  219.    xprintf (f, "\n");
  220.    row++;
  221. }
  222.  
  223.  
  224. local void Staticfunc task_group (int f, gcbptr base, word oldpri)
  225. {
  226.    queptr curr;
  227.    tcbptr task;
  228.    gcbptr group;
  229.  
  230.    for (group = base; group != LNULL; group = group->level)
  231.       {
  232.       dgroup (f, group);
  233.       for (curr = group->namelist.list.first; !(curr->kind & Q_HEAD);
  234.            curr = curr->next)
  235.          if (curr->kind == TYP_TCB)
  236.             {
  237.             task = (tcbptr) ((nameptr)curr)->strucp;
  238.  
  239.             if (xprintf (f, "%-8Fs %-5Fs %Fp ",
  240.                          ((nameptr)curr)->name, 
  241.                          (farptr) sn_state [(task->state <= 5) 
  242.                                             ? task->state : 0],
  243.                          task->qhead))
  244.                return;
  245.  
  246.             if (task->timerq.link.next == LNULL)
  247.                xprintf (f, "%8c", '-');
  248.             else switch (task->timerq.link.kind)
  249.                {
  250.                case TYP_WATCH:         xprintf (f, " Watch ");
  251.                                        if ((task->timerq.elkind & 0xf0)
  252.                                            == TELEM_MEM)
  253.                                           xprintf (f, "M");
  254.                                        else
  255.                                           xprintf (f, "P");
  256.                                        break;
  257.  
  258.                case TYP_HOTKEY:        xprintf (f, " Hotkey ");
  259.                                        break;
  260.  
  261.                case TYP_TIMER:         xprintf (f, "%8ld", 
  262.                                              task->timerq.link.el.ticks);
  263.                                        break;
  264.  
  265.                default:                xprintf (f, "  ?%5d",
  266.                                                 task->timerq.link.kind); 
  267.                                        break;
  268.                }
  269.  
  270. #if (CHECKING)
  271. #if (!DBPSP)
  272.             xprintf (f, " %Fp %Fp   %6d  %Fp",
  273.                   task,
  274.                   task->stack, 
  275.                   stack_free (task),
  276.                   ((struct task_stack far *)task->stack)->retn);
  277. #else
  278.             xprintf (f, " %Fp %04X %04X   %6d  %Fp",
  279.                   task,
  280.                   task->base_psp,
  281.                   *((word far *)(&task->swap_area [0x0e])),
  282.                   stack_free (task),
  283.                   ((struct task_stack far *)task->stack)->retn);
  284. #endif
  285. #else
  286. #if (!DBPSP)
  287.             xprintf (f, " %Fp %Fp %Fp %Fp",
  288.                   task,
  289.                   task->stack, 
  290.                   task->stkbot,
  291.                   ((struct task_stack far *)task->stack)->retn);
  292. #else
  293.             xprintf (f, " %Fp %04X %04X %Fp %Fp",
  294.                   task,
  295.                   task->base_psp,
  296.                   *((word far *)(&task->swap_area [0x0e])),
  297.                   task->psp_sssp,
  298.                   ((struct task_stack far *)task->stack)->retn);
  299. #endif
  300. #endif
  301.             if (task != GLOBDATA current_task)
  302.                xprintf (f, " %5u\n", task->cqueue.el.pri.prior);
  303.             else
  304.                xprintf (f, "*%5u\n", oldpri);
  305.             row++;
  306.             }
  307.  
  308.       if (group->branch != LNULL)
  309.          task_group (f, group->branch, oldpri);
  310.       }
  311. }
  312.  
  313.  
  314. local void Staticfunc dump_tasks (int f, word oldpri)
  315. {
  316.    static union REGS regs;
  317.  
  318.    regs.x.ax = 0x1683;
  319.    int86 (0x2f, ®s, ®s);
  320.    row++;
  321.    xprintf (f, "\nTask List (VM=%04X):\n", regs.x.bx);
  322.    row++;
  323. #if (CHECKING)
  324. #if (!DBPSP)
  325.    xprintf (f, "Name     State Queue      Timeout TCB-addr  Stackptr  FreeStack Instrptr  Prior\n");
  326. #else
  327.    xprintf (f, "Name     State Queue      Timeout TCB-addr  BPSP CPSP FreeStack Instrptr  Prior\n");
  328. #endif
  329. #else
  330. #if (!DBPSP)
  331.    xprintf (f, "Name     State Queue      Timeout TCB-addr  Stackptr  Stackbot  Instrptr  Prior\n");
  332. #else
  333.    xprintf (f, "Name     State Queue      Timeout TCB-addr  BPSP CPSP PSP-SSSP  Instrptr  Prior\n");
  334. #endif
  335. #endif
  336.    row++;
  337.  
  338.    task_group (f, &GLOBDATA group, oldpri);
  339.    xprintf (f, "\n");
  340.    row++;
  341. }
  342.  
  343.  
  344. local void Staticfunc ssnap (int f)
  345. {
  346.    word oldpri;
  347.  
  348.    oldpri = get_priority (GLOBDATA current_task);
  349.    set_priority (GLOBDATA current_task, 0xffff);
  350.  
  351.    row = 0;
  352.    dump_tasks (f, oldpri);
  353.    dump_events (f);
  354.  
  355.    set_priority (GLOBDATA current_task, oldpri);
  356. }
  357.  
  358.  
  359. #if (!NOSTDIO)
  360.  
  361. void Globalfunc snapshot (FILE *f)
  362. {
  363.    fflush (f);
  364.    maxrow = 0;
  365.    ssnap (fileno (f));
  366. }
  367.  
  368. #endif
  369.  
  370. void Globalfunc csnapshot (void)
  371. {
  372.    maxrow = 0;
  373.    ssnap (-1);
  374. }
  375.  
  376.  
  377. void Globalfunc comsnapshot ()
  378. {
  379.    maxrow = 0;
  380.    ssnap (-3);
  381. }
  382.  
  383.  
  384. void Globalfunc screensnap (int rows)
  385. {
  386. #if (DEBUG)
  387.    maxrow = rows - 3;   /* Leave 2 lines at top alone */
  388.    tsk_setpos (2, 0);
  389. #else
  390.    maxrow = rows - 1;
  391.    tsk_setpos (0, 0);
  392. #endif
  393.    tsk_set_clreol (1);
  394.    ssnap (-2);
  395. }
  396.  
  397. #endif
  398.  
  399.