3 * kProfiler Mark 2 - Core Termination Code Template.
7 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
33 * Unwinds and terminates all the threads, and frees the stack space.
35 * @returns The new data set size. (pHdr->cb)
36 * @param pHdr The profiler data set header.
38 KPRF_DECL_FUNC(KU32
, TerminateAll
)(KPRF_TYPE(P
,HDR
) pHdr
)
45 * Iterate the threads and terminate all which are non-terminated.
47 KPRF_TYPE(P
,THREAD
) paThread
= KPRF_OFF2PTR(P
,THREAD
, pHdr
->offThreads
, pHdr
);
48 for (KU32 i
= 0; i
< pHdr
->cThreads
; i
++)
50 KPRF_TYPE(P
,THREAD
) pCur
= &paThread
[i
];
51 switch (pCur
->enmState
)
53 /* these states needs no work. */
54 case KPRF_TYPE(,THREADSTATE_TERMINATED
):
55 case KPRF_TYPE(,THREADSTATE_UNUSED
):
59 /* these are active and requires unwinding.*/
60 case KPRF_TYPE(,THREADSTATE_ACTIVE
):
61 case KPRF_TYPE(,THREADSTATE_SUSPENDED
):
62 case KPRF_TYPE(,THREADSTATE_OVERFLOWED
):
63 KPRF_NAME(TerminateThread
)(pHdr
, pCur
, TS
);
74 /* only if the stack is at the end of the data set. */
75 const KU32 cbStacks
= KPRF_ALIGN(pHdr
->cMaxStacks
* pHdr
->cbStack
, 32);
76 if (pHdr
->offStacks
+ cbStacks
== pHdr
->cb
)
86 * Sets the commandline.
88 * This is typically done after TerminateAll, when the stacks has
89 * been freed up and there is plenty free space.
91 * @returns The new data set size. (pHdr->cb)
92 * @param pHdr The profiler data set header.
93 * @param cArgs The number of arguments in the array.
94 * @param papszArgs Pointer to an array of arguments.
96 KPRF_DECL_FUNC(KU32
, SetCommandLine
)(KPRF_TYPE(P
,HDR
) pHdr
, unsigned cArgs
, const char * const *papszArgs
)
104 if (pHdr
->cb
+ 16 > pHdr
->cbAllocated
) /* 16 bytes min */
108 * Encode untill we run out of space.
110 pHdr
->offCommandLine
= pHdr
->cb
;
111 char *psz
= (char *)pHdr
+ pHdr
->cb
;
112 char *pszMax
= (char *)pHdr
+ pHdr
->cbAllocated
- 1;
113 for (unsigned i
= 0; i
< cArgs
&& psz
+ 7 < pszMax
; i
++)
118 const char *pszArg
= papszArgs
[i
];
126 if (psz
+ 1 >= pszMax
)
136 pHdr
->cb
= psz
- (char *)pHdr
;
137 pHdr
->cchCommandLine
= pHdr
->cb
- pHdr
->offCommandLine
- 1;