summaryrefslogtreecommitdiff
path: root/minix/lib/libmthread/misc.c
blob: 8a4ad3fbabe1b72f151dcb8b96922406f6220823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <minix/mthread.h>
#include <stdio.h>
#include "global.h"
#include "proto.h"

/*===========================================================================*
 *				mthread_debug_f				     *
 *===========================================================================*/
#ifdef MDEBUG
void mthread_debug_f(const char *file, int line, const char *msg)
{
  /* Print debug message */
  printf("MTH (%s:%d): %s\n", file, line, msg);
}
#endif

/*===========================================================================*
 *				mthread_panic_f				     *
 *===========================================================================*/
#ifdef MDEBUG
void mthread_panic_f(const char *file, int line, const char *msg)
{
  /* Print panic message to stdout and exit */
  volatile int *sf;

  sf = NULL;

  printf("mthread panic (%s:%d): ", file, line);
  printf("%s", msg);
  printf("\n");
  fflush(stdout);	/* Force debug print to screen */
  *((int *) sf ) = 1;	/* Cause segfault to generate trace */
  exit(1);
}
#else
void mthread_panic_s(void)
{
  /* Silent panic */
  volatile int *sf;

  sf = NULL;
  *((volatile int *) sf ) = 1;	/* Cause segfault to generate trace */
  exit(1);
}
#endif


/*===========================================================================*
 *				mthread_verify_f			     *
 *===========================================================================*/
#ifdef MDEBUG
void mthread_verify_f(char *file, int line)
{
  /* Verify library state. It is assumed this function is never called from
   * a spawned thread, but from the 'main' thread. The library should be 
   * quiescent; no mutexes, conditions, or threads in use. All threads are to
   * be in DEAD state.
   */
  mthread_thread_t t;
  mthread_tcb_t *tcb;
  int threads_ok = 1, conditions_ok = 1, mutexes_ok = 1, attributes_ok = 1;

  for (t = (mthread_thread_t) 0; threads_ok && t < no_threads; t++) {
  	tcb = mthread_find_tcb(t);
  	if (tcb->m_state != MS_DEAD) threads_ok = 0;
  }

  conditions_ok = mthread_cond_verify();
  mutexes_ok = mthread_mutex_verify();
  attributes_ok = mthread_attr_verify();

  printf("(%s:%d) VERIFY ", file, line);
  printf("| threads: %s |", (threads_ok ? "ok": "NOT ok"));
  printf("| cond: %s |", (conditions_ok ? "ok": "NOT ok"));
  printf("| mutex: %s |", (mutexes_ok ? "ok": "NOT ok"));
  printf("| attr: %s |", (attributes_ok ? "ok": "NOT ok"));
  printf("\n");

  if(!threads_ok || !conditions_ok || !mutexes_ok)
	mthread_panic("Library state corrupt\n");
}


/*===========================================================================*
 *				mthread_stats				     *
 *===========================================================================*/
void mthread_stats(void)
{
  mthread_thread_t t;
  mthread_tcb_t *tcb;
  int st_run, st_dead, st_cond, st_mutex, st_exit;
  st_run = st_dead = st_cond = st_mutex = st_exit = 0;

  for (t = (mthread_thread_t) 0; t < no_threads; t++) {
  	tcb = mthread_find_tcb(t);
  	switch(tcb->m_state) {
  		case MS_RUNNABLE: st_run++; break;
  		case MS_DEAD: st_dead++; break;
  		case MS_MUTEX: st_mutex++; break;
  		case MS_CONDITION: st_cond++; break;
  		case MS_EXITING: st_exit++; break;
  		default: mthread_panic("Unknown state");
  	}
  }

  printf("Pool: %-5d In use: %-5d R: %-5d D: %-5d M: %-5d C: %-5d E: %-5d\n",
  	 no_threads, used_threads, st_run, st_dead, st_mutex, st_cond,
	 st_exit);
}

#endif


/*===========================================================================*
 *				mthread_stacktrace			     *
 *===========================================================================*/
void mthread_stacktrace(mthread_thread_t t)
{
#ifdef __i386__ /* stacktrace only implemented on x86 */
  unsigned long bp, hbp, pc;
  mthread_tcb_t *tcb;
  ucontext_t *ctx;

  tcb = mthread_find_tcb(t);
  ctx = &tcb->m_context;

  if (t != MAIN_THREAD && ctx->uc_stack.ss_size == 0)
	return; /* no stack, no stacktrace */

  printf("thread %d: ", t);

  bp = _UC_MACHINE_EBP(ctx);

  while (bp) {
	pc = ((unsigned long *) bp)[1];
	hbp = ((unsigned long *) bp)[0];

	printf("0x%lx ", (unsigned long) pc);

	if (hbp != 0 && hbp <= bp) {
		pc = -1;
		printf("0x%lx ", (unsigned long) pc);
		break;
	}
	bp = hbp;
  }

  printf("\n");
#endif
}

/*===========================================================================*
 *				mthread_stacktraces			     *
 *===========================================================================*/
void mthread_stacktraces(void)
{
  mthread_thread_t t;

  mthread_stacktrace(MAIN_THREAD);

  for (t = (mthread_thread_t) 0; t < no_threads; t++)
	mthread_stacktrace(t);
}