8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / libelf / demo / tpcom.c
blob56a28dfab0e222b0cf24f396dbb0dd25609b0145
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
26 * tpcom: Threaded Print Comment
28 * tpcom is a threaded version of the pcom program. It will create
29 * a new thread for each new ELF descriptor that it examines. It
30 * will then examine each elf descriptor and print the .comment section
31 * if found.
33 * This program demonstrates that libelf is MT-Safe and the usage
34 * of elf_begin(ELF_C_READ).
37 #include <stdio.h>
38 #include <libelf.h>
39 #include <gelf.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <thread.h>
47 #define NUMLWPS 32 /* arbitrary number of LWPS */
49 static const char *CommentStr = ".comment";
52 * arguments to be passed into process_elf().
54 typedef struct {
55 Elf *pe_elf;
56 char *pe_file; /* elf member name */
57 int pe_fd;
58 short pe_member; /* is this an archive member? */
59 } pe_args;
62 static mutex_t printlock = DEFAULTMUTEX; /* printlock used to */
63 /* group output */
64 /* of comment sections */
66 static void
67 print_comment(Elf *elf, const char *file)
69 Elf_Scn *scn = NULL;
70 GElf_Shdr shdr;
71 Elf_Data *data;
72 size_t shstrndx;
74 if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
75 (void) fprintf(stderr, "%s: elf_getshdrstrndx() failed: %s\n",
76 file, elf_errmsg(0));
77 return;
80 while ((scn = elf_nextscn(elf, scn)) != NULL) {
82 * Do a string compare to examine each section header
83 * to see if it is a ".comment" section. If it is then
84 * this is the section we want to process.
86 if (gelf_getshdr(scn, &shdr) == NULL) {
87 (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n",
88 file, elf_errmsg(0));
89 return;
92 if (strcmp(CommentStr, elf_strptr(elf, shstrndx,
93 shdr.sh_name)) == 0) {
94 int i;
95 char *ptr;
97 (void) mutex_lock(&printlock);
98 (void) printf("%s .comment:\n", file);
101 * Get the data associated with the .comment
102 * section.
104 if ((data = elf_getdata(scn, NULL)) == NULL) {
105 (void) fprintf(stderr,
106 "%s: elf_getdata() failed: %s\n",
107 file, elf_errmsg(0));
108 (void) mutex_unlock(&printlock);
109 return;
112 * Data in a .comment section is a list of 'null'
113 * terminated strings. The following will print
114 * one string per line.
116 for (i = 0, ptr = (char *)data->d_buf;
117 i < data->d_size; i++)
118 if (ptr[i]) {
119 (void) puts(&ptr[i]);
120 i += strlen(&ptr[i]);
122 (void) putchar('\n');
123 (void) mutex_unlock(&printlock);
129 static void
130 process_elf(pe_args *pep)
132 Elf_Cmd cmd;
133 Elf *_elf;
135 switch (elf_kind(pep->pe_elf)) {
136 case ELF_K_ELF:
137 print_comment(pep->pe_elf, pep->pe_file);
138 break;
139 case ELF_K_AR:
140 cmd = ELF_C_READ;
141 while ((_elf = elf_begin(pep->pe_fd, cmd,
142 pep->pe_elf)) != NULL) {
143 Elf_Arhdr *arhdr;
144 pe_args *_pep;
145 int rc;
147 if ((arhdr = elf_getarhdr(_elf)) == NULL) {
148 (void) fprintf(stderr,
149 "%s: elf_getarhdr() failed: %s\n",
150 pep->pe_file, elf_errmsg(0));
152 cmd = elf_next(_elf);
153 _pep = malloc(sizeof (pe_args));
154 _pep->pe_elf = _elf;
155 _pep->pe_file = malloc(strlen(pep->pe_file) +
156 strlen(arhdr->ar_name) + 5);
157 (void) sprintf(_pep->pe_file,
158 "%s(%s)", pep->pe_file, arhdr->ar_name);
159 _pep->pe_fd = pep->pe_fd;
160 _pep->pe_member = 1;
162 if ((rc = thr_create(NULL, 0,
163 (void *(*)(void *))process_elf,
164 (void *)_pep, THR_DETACHED, 0)) != 0) {
165 (void) fprintf(stderr,
166 "thr_create() failed, rc = %d\n", rc);
169 break;
170 default:
171 if (!pep->pe_member) {
172 (void) mutex_lock(&printlock);
173 (void) fprintf(stderr,
174 "%s: unexpected elf_kind(): 0x%x\n",
175 pep->pe_file, elf_kind(pep->pe_elf));
176 (void) mutex_unlock(&printlock);
180 (void) elf_end(pep->pe_elf);
181 if (pep->pe_member)
182 free(pep->pe_file);
183 free(pep);
184 thr_exit(0);
188 main(int argc, char ** argv)
190 int i;
192 if (argc < 2) {
193 (void) printf("usage: %s elf_file ...\n", argv[0]);
194 return (1);
198 * Initialize the elf library, must be called before elf_begin()
199 * can be called.
201 if (elf_version(EV_CURRENT) == EV_NONE) {
202 (void) fprintf(stderr,
203 "elf_version() failed: %s\n", elf_errmsg(0));
204 return (1);
208 * create an arbitrary number of LWP's to run the
209 * threads that will be created.
211 if (thr_setconcurrency(NUMLWPS) != 0) {
212 (void) fprintf(stderr, "thread setconcurrency failed\n");
213 return (1);
216 for (i = 1; i < argc; i++) {
217 int fd;
218 Elf *elf;
219 pe_args *pep;
220 int rc;
221 char *elf_fname;
223 elf_fname = argv[i];
225 if ((fd = open(elf_fname, O_RDONLY)) == -1) {
226 perror("open");
227 continue;
231 * Attempt to open an Elf descriptor Read/Write
232 * for each file.
234 if ((elf = elf_begin(fd, ELF_C_READ, 0)) == NULL) {
235 (void) mutex_lock(&printlock);
236 (void) fprintf(stderr, "elf_begin() failed: %s\n",
237 elf_errmsg(0));
238 (void) mutex_unlock(&printlock);
239 (void) close(fd);
240 continue;
242 pep = malloc(sizeof (pe_args));
243 pep->pe_elf = elf;
244 pep->pe_file = elf_fname;
245 pep->pe_fd = fd;
246 pep->pe_member = 0;
247 if ((rc = thr_create(NULL, 0, (void *(*)(void *))process_elf,
248 (void *)pep, THR_DETACHED, 0)) != 0) {
249 (void) mutex_lock(&printlock);
250 (void) fprintf(stderr,
251 "thr_create() failed with code: %d\n", rc);
252 (void) mutex_unlock(&printlock);
253 return (1);
257 thr_exit(0);
258 return (0);