1 | /* $Id: prfcoreterm.cpp.h 2 2007-11-16 16:07:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kProfiler Mark 2 - Core Termination Code Template.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * This file is part of kProfiler.
|
---|
10 | *
|
---|
11 | * kProfiler is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU Lesser General Public
|
---|
13 | * License as published by the Free Software Foundation; either
|
---|
14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kProfiler is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * Lesser General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU Lesser General Public
|
---|
22 | * License along with kProfiler; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Unwinds and terminates all the threads, and frees the stack space.
|
---|
30 | *
|
---|
31 | * @returns The new data set size. (pHdr->cb)
|
---|
32 | * @param pHdr The profiler data set header.
|
---|
33 | */
|
---|
34 | KPRF_DECL_FUNC(KU32, TerminateAll)(KPRF_TYPE(P,HDR) pHdr)
|
---|
35 | {
|
---|
36 | KU64 TS = KPRF_NOW();
|
---|
37 | if (!pHdr)
|
---|
38 | return 0;
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * Iterate the threads and terminate all which are non-terminated.
|
---|
42 | */
|
---|
43 | KPRF_TYPE(P,THREAD) paThread = KPRF_OFF2PTR(P,THREAD, pHdr->offThreads, pHdr);
|
---|
44 | for (KU32 i = 0; i < pHdr->cThreads; i++)
|
---|
45 | {
|
---|
46 | KPRF_TYPE(P,THREAD) pCur = &paThread[i];
|
---|
47 | switch (pCur->enmState)
|
---|
48 | {
|
---|
49 | /* these states needs no work. */
|
---|
50 | case KPRF_TYPE(,THREADSTATE_TERMINATED):
|
---|
51 | case KPRF_TYPE(,THREADSTATE_UNUSED):
|
---|
52 | default:
|
---|
53 | break;
|
---|
54 |
|
---|
55 | /* these are active and requires unwinding.*/
|
---|
56 | case KPRF_TYPE(,THREADSTATE_ACTIVE):
|
---|
57 | case KPRF_TYPE(,THREADSTATE_SUSPENDED):
|
---|
58 | case KPRF_TYPE(,THREADSTATE_OVERFLOWED):
|
---|
59 | KPRF_NAME(TerminateThread)(pHdr, pCur, TS);
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Free the stacks.
|
---|
67 | */
|
---|
68 | if (pHdr->offStacks)
|
---|
69 | {
|
---|
70 | /* only if the stack is at the end of the data set. */
|
---|
71 | const KU32 cbStacks = KPRF_ALIGN(pHdr->cMaxStacks * pHdr->cbStack, 32);
|
---|
72 | if (pHdr->offStacks + cbStacks == pHdr->cb)
|
---|
73 | pHdr->cb -= cbStacks;
|
---|
74 | pHdr->offStacks = 0;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return pHdr->cb;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Sets the commandline.
|
---|
83 | *
|
---|
84 | * This is typically done after TerminateAll, when the stacks has
|
---|
85 | * been freed up and there is plenty free space.
|
---|
86 | *
|
---|
87 | * @returns The new data set size. (pHdr->cb)
|
---|
88 | * @param pHdr The profiler data set header.
|
---|
89 | * @param cArgs The number of arguments in the array.
|
---|
90 | * @param papszArgs Pointer to an array of arguments.
|
---|
91 | */
|
---|
92 | KPRF_DECL_FUNC(KU32, SetCommandLine)(KPRF_TYPE(P,HDR) pHdr, unsigned cArgs, const char * const *papszArgs)
|
---|
93 | {
|
---|
94 | if (!pHdr)
|
---|
95 | return 0;
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Any space at all?
|
---|
99 | */
|
---|
100 | if (pHdr->cb + 16 > pHdr->cbAllocated) /* 16 bytes min */
|
---|
101 | return pHdr->cb;
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Encode untill we run out of space.
|
---|
105 | */
|
---|
106 | pHdr->offCommandLine = pHdr->cb;
|
---|
107 | char *psz = (char *)pHdr + pHdr->cb;
|
---|
108 | char *pszMax = (char *)pHdr + pHdr->cbAllocated - 1;
|
---|
109 | for (unsigned i = 0; i < cArgs && psz + 7 < pszMax; i++)
|
---|
110 | {
|
---|
111 | if (i > 0)
|
---|
112 | *psz++ = ' ';
|
---|
113 | *psz++ = '\'';
|
---|
114 | const char *pszArg = papszArgs[i];
|
---|
115 | while (psz < pszMax)
|
---|
116 | {
|
---|
117 | char ch = *pszArg++;
|
---|
118 | if (!ch)
|
---|
119 | break;
|
---|
120 | if (ch == '\'')
|
---|
121 | {
|
---|
122 | if (psz + 1 >= pszMax)
|
---|
123 | break;
|
---|
124 | *psz++ = '\\';
|
---|
125 | }
|
---|
126 | *psz++ = ch;
|
---|
127 | }
|
---|
128 | if (psz < pszMax)
|
---|
129 | *psz++ = '\'';
|
---|
130 | }
|
---|
131 | *psz++ = '\0';
|
---|
132 | pHdr->cb = psz - (char *)pHdr;
|
---|
133 | pHdr->cchCommandLine = pHdr->cb - pHdr->offCommandLine - 1;
|
---|
134 |
|
---|
135 | return pHdr->cb;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|