debug.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. /*
  2. * arch/s390/kernel/debug.c
  3. * S/390 debug facility
  4. *
  5. * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Michael Holzheu (holzheu@de.ibm.com),
  8. * Holger Smolinski (Holger.Smolinski@de.ibm.com)
  9. *
  10. * Bugreports to: <Linux390@de.ibm.com>
  11. */
  12. #include <linux/stddef.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/ctype.h>
  17. #include <linux/sysctl.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/semaphore.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/fs.h>
  23. #include <linux/debugfs.h>
  24. #include <asm/debug.h>
  25. #define DEBUG_PROLOG_ENTRY -1
  26. #define ALL_AREAS 0 /* copy all debug areas */
  27. #define NO_AREAS 1 /* copy no debug areas */
  28. /* typedefs */
  29. typedef struct file_private_info {
  30. loff_t offset; /* offset of last read in file */
  31. int act_area; /* number of last formated area */
  32. int act_page; /* act page in given area */
  33. int act_entry; /* last formated entry (offset */
  34. /* relative to beginning of last */
  35. /* formated page) */
  36. size_t act_entry_offset; /* up to this offset we copied */
  37. /* in last read the last formated */
  38. /* entry to userland */
  39. char temp_buf[2048]; /* buffer for output */
  40. debug_info_t *debug_info_org; /* original debug information */
  41. debug_info_t *debug_info_snap; /* snapshot of debug information */
  42. struct debug_view *view; /* used view of debug info */
  43. } file_private_info_t;
  44. typedef struct
  45. {
  46. char *string;
  47. /*
  48. * This assumes that all args are converted into longs
  49. * on L/390 this is the case for all types of parameter
  50. * except of floats, and long long (32 bit)
  51. *
  52. */
  53. long args[0];
  54. } debug_sprintf_entry_t;
  55. extern void tod_to_timeval(uint64_t todval, struct timespec *xtime);
  56. /* internal function prototyes */
  57. static int debug_init(void);
  58. static ssize_t debug_output(struct file *file, char __user *user_buf,
  59. size_t user_len, loff_t * offset);
  60. static ssize_t debug_input(struct file *file, const char __user *user_buf,
  61. size_t user_len, loff_t * offset);
  62. static int debug_open(struct inode *inode, struct file *file);
  63. static int debug_close(struct inode *inode, struct file *file);
  64. static debug_info_t* debug_info_create(char *name, int pages_per_area,
  65. int nr_areas, int buf_size);
  66. static void debug_info_get(debug_info_t *);
  67. static void debug_info_put(debug_info_t *);
  68. static int debug_prolog_level_fn(debug_info_t * id,
  69. struct debug_view *view, char *out_buf);
  70. static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
  71. struct file *file, const char __user *user_buf,
  72. size_t user_buf_size, loff_t * offset);
  73. static int debug_prolog_pages_fn(debug_info_t * id,
  74. struct debug_view *view, char *out_buf);
  75. static int debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
  76. struct file *file, const char __user *user_buf,
  77. size_t user_buf_size, loff_t * offset);
  78. static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
  79. struct file *file, const char __user *user_buf,
  80. size_t user_buf_size, loff_t * offset);
  81. static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
  82. char *out_buf, const char *in_buf);
  83. static int debug_raw_format_fn(debug_info_t * id,
  84. struct debug_view *view, char *out_buf,
  85. const char *in_buf);
  86. static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
  87. int area, debug_entry_t * entry, char *out_buf);
  88. static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
  89. char *out_buf, debug_sprintf_entry_t *curr_event);
  90. /* globals */
  91. struct debug_view debug_raw_view = {
  92. "raw",
  93. NULL,
  94. &debug_raw_header_fn,
  95. &debug_raw_format_fn,
  96. NULL,
  97. NULL
  98. };
  99. struct debug_view debug_hex_ascii_view = {
  100. "hex_ascii",
  101. NULL,
  102. &debug_dflt_header_fn,
  103. &debug_hex_ascii_format_fn,
  104. NULL,
  105. NULL
  106. };
  107. struct debug_view debug_level_view = {
  108. "level",
  109. &debug_prolog_level_fn,
  110. NULL,
  111. NULL,
  112. &debug_input_level_fn,
  113. NULL
  114. };
  115. struct debug_view debug_pages_view = {
  116. "pages",
  117. &debug_prolog_pages_fn,
  118. NULL,
  119. NULL,
  120. &debug_input_pages_fn,
  121. NULL
  122. };
  123. struct debug_view debug_flush_view = {
  124. "flush",
  125. NULL,
  126. NULL,
  127. NULL,
  128. &debug_input_flush_fn,
  129. NULL
  130. };
  131. struct debug_view debug_sprintf_view = {
  132. "sprintf",
  133. NULL,
  134. &debug_dflt_header_fn,
  135. (debug_format_proc_t*)&debug_sprintf_format_fn,
  136. NULL,
  137. NULL
  138. };
  139. unsigned int debug_feature_version = __DEBUG_FEATURE_VERSION;
  140. /* static globals */
  141. static debug_info_t *debug_area_first = NULL;
  142. static debug_info_t *debug_area_last = NULL;
  143. DECLARE_MUTEX(debug_lock);
  144. static int initialized;
  145. static struct file_operations debug_file_ops = {
  146. .owner = THIS_MODULE,
  147. .read = debug_output,
  148. .write = debug_input,
  149. .open = debug_open,
  150. .release = debug_close,
  151. };
  152. static struct dentry *debug_debugfs_root_entry;
  153. /* functions */
  154. /*
  155. * debug_areas_alloc
  156. * - Debug areas are implemented as a threedimensonal array:
  157. * areas[areanumber][pagenumber][pageoffset]
  158. */
  159. static debug_entry_t***
  160. debug_areas_alloc(int pages_per_area, int nr_areas)
  161. {
  162. debug_entry_t*** areas;
  163. int i,j;
  164. areas = (debug_entry_t ***) kmalloc(nr_areas *
  165. sizeof(debug_entry_t**),
  166. GFP_KERNEL);
  167. if (!areas)
  168. goto fail_malloc_areas;
  169. for (i = 0; i < nr_areas; i++) {
  170. areas[i] = (debug_entry_t**) kmalloc(pages_per_area *
  171. sizeof(debug_entry_t*),GFP_KERNEL);
  172. if (!areas[i]) {
  173. goto fail_malloc_areas2;
  174. }
  175. for(j = 0; j < pages_per_area; j++) {
  176. areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
  177. if(!areas[i][j]) {
  178. for(j--; j >=0 ; j--) {
  179. kfree(areas[i][j]);
  180. }
  181. kfree(areas[i]);
  182. goto fail_malloc_areas2;
  183. }
  184. }
  185. }
  186. return areas;
  187. fail_malloc_areas2:
  188. for(i--; i >= 0; i--){
  189. for(j=0; j < pages_per_area;j++){
  190. kfree(areas[i][j]);
  191. }
  192. kfree(areas[i]);
  193. }
  194. kfree(areas);
  195. fail_malloc_areas:
  196. return NULL;
  197. }
  198. /*
  199. * debug_info_alloc
  200. * - alloc new debug-info
  201. */
  202. static debug_info_t*
  203. debug_info_alloc(char *name, int pages_per_area, int nr_areas, int buf_size,
  204. int level, int mode)
  205. {
  206. debug_info_t* rc;
  207. /* alloc everything */
  208. rc = (debug_info_t*) kmalloc(sizeof(debug_info_t), GFP_KERNEL);
  209. if(!rc)
  210. goto fail_malloc_rc;
  211. rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  212. if(!rc->active_entries)
  213. goto fail_malloc_active_entries;
  214. rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  215. if(!rc->active_pages)
  216. goto fail_malloc_active_pages;
  217. if((mode == ALL_AREAS) && (pages_per_area != 0)){
  218. rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
  219. if(!rc->areas)
  220. goto fail_malloc_areas;
  221. } else {
  222. rc->areas = NULL;
  223. }
  224. /* initialize members */
  225. spin_lock_init(&rc->lock);
  226. rc->pages_per_area = pages_per_area;
  227. rc->nr_areas = nr_areas;
  228. rc->active_area = 0;
  229. rc->level = level;
  230. rc->buf_size = buf_size;
  231. rc->entry_size = sizeof(debug_entry_t) + buf_size;
  232. strlcpy(rc->name, name, sizeof(rc->name)-1);
  233. memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
  234. memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
  235. sizeof(struct dentry*));
  236. atomic_set(&(rc->ref_count), 0);
  237. return rc;
  238. fail_malloc_areas:
  239. kfree(rc->active_pages);
  240. fail_malloc_active_pages:
  241. kfree(rc->active_entries);
  242. fail_malloc_active_entries:
  243. kfree(rc);
  244. fail_malloc_rc:
  245. return NULL;
  246. }
  247. /*
  248. * debug_areas_free
  249. * - free all debug areas
  250. */
  251. static void
  252. debug_areas_free(debug_info_t* db_info)
  253. {
  254. int i,j;
  255. if(!db_info->areas)
  256. return;
  257. for (i = 0; i < db_info->nr_areas; i++) {
  258. for(j = 0; j < db_info->pages_per_area; j++) {
  259. kfree(db_info->areas[i][j]);
  260. }
  261. kfree(db_info->areas[i]);
  262. }
  263. kfree(db_info->areas);
  264. db_info->areas = NULL;
  265. }
  266. /*
  267. * debug_info_free
  268. * - free memory debug-info
  269. */
  270. static void
  271. debug_info_free(debug_info_t* db_info){
  272. debug_areas_free(db_info);
  273. kfree(db_info->active_entries);
  274. kfree(db_info->active_pages);
  275. kfree(db_info);
  276. }
  277. /*
  278. * debug_info_create
  279. * - create new debug-info
  280. */
  281. static debug_info_t*
  282. debug_info_create(char *name, int pages_per_area, int nr_areas, int buf_size)
  283. {
  284. debug_info_t* rc;
  285. rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
  286. DEBUG_DEFAULT_LEVEL, ALL_AREAS);
  287. if(!rc)
  288. goto out;
  289. /* create root directory */
  290. rc->debugfs_root_entry = debugfs_create_dir(rc->name,
  291. debug_debugfs_root_entry);
  292. /* append new element to linked list */
  293. if (!debug_area_first) {
  294. /* first element in list */
  295. debug_area_first = rc;
  296. rc->prev = NULL;
  297. } else {
  298. /* append element to end of list */
  299. debug_area_last->next = rc;
  300. rc->prev = debug_area_last;
  301. }
  302. debug_area_last = rc;
  303. rc->next = NULL;
  304. debug_info_get(rc);
  305. out:
  306. return rc;
  307. }
  308. /*
  309. * debug_info_copy
  310. * - copy debug-info
  311. */
  312. static debug_info_t*
  313. debug_info_copy(debug_info_t* in, int mode)
  314. {
  315. int i,j;
  316. debug_info_t* rc;
  317. unsigned long flags;
  318. /* get a consistent copy of the debug areas */
  319. do {
  320. rc = debug_info_alloc(in->name, in->pages_per_area,
  321. in->nr_areas, in->buf_size, in->level, mode);
  322. spin_lock_irqsave(&in->lock, flags);
  323. if(!rc)
  324. goto out;
  325. /* has something changed in the meantime ? */
  326. if((rc->pages_per_area == in->pages_per_area) &&
  327. (rc->nr_areas == in->nr_areas)) {
  328. break;
  329. }
  330. spin_unlock_irqrestore(&in->lock, flags);
  331. debug_info_free(rc);
  332. } while (1);
  333. if(!rc || (mode == NO_AREAS))
  334. goto out;
  335. for(i = 0; i < in->nr_areas; i++){
  336. for(j = 0; j < in->pages_per_area; j++) {
  337. memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
  338. }
  339. }
  340. out:
  341. spin_unlock_irqrestore(&in->lock, flags);
  342. return rc;
  343. }
  344. /*
  345. * debug_info_get
  346. * - increments reference count for debug-info
  347. */
  348. static void
  349. debug_info_get(debug_info_t * db_info)
  350. {
  351. if (db_info)
  352. atomic_inc(&db_info->ref_count);
  353. }
  354. /*
  355. * debug_info_put:
  356. * - decreases reference count for debug-info and frees it if necessary
  357. */
  358. static void
  359. debug_info_put(debug_info_t *db_info)
  360. {
  361. int i;
  362. if (!db_info)
  363. return;
  364. if (atomic_dec_and_test(&db_info->ref_count)) {
  365. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  366. if (!db_info->views[i])
  367. continue;
  368. debugfs_remove(db_info->debugfs_entries[i]);
  369. }
  370. debugfs_remove(db_info->debugfs_root_entry);
  371. if(db_info == debug_area_first)
  372. debug_area_first = db_info->next;
  373. if(db_info == debug_area_last)
  374. debug_area_last = db_info->prev;
  375. if(db_info->prev) db_info->prev->next = db_info->next;
  376. if(db_info->next) db_info->next->prev = db_info->prev;
  377. debug_info_free(db_info);
  378. }
  379. }
  380. /*
  381. * debug_format_entry:
  382. * - format one debug entry and return size of formated data
  383. */
  384. static int
  385. debug_format_entry(file_private_info_t *p_info)
  386. {
  387. debug_info_t *id_snap = p_info->debug_info_snap;
  388. struct debug_view *view = p_info->view;
  389. debug_entry_t *act_entry;
  390. size_t len = 0;
  391. if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
  392. /* print prolog */
  393. if (view->prolog_proc)
  394. len += view->prolog_proc(id_snap,view,p_info->temp_buf);
  395. goto out;
  396. }
  397. if (!id_snap->areas) /* this is true, if we have a prolog only view */
  398. goto out; /* or if 'pages_per_area' is 0 */
  399. act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
  400. [p_info->act_page] + p_info->act_entry);
  401. if (act_entry->id.stck == 0LL)
  402. goto out; /* empty entry */
  403. if (view->header_proc)
  404. len += view->header_proc(id_snap, view, p_info->act_area,
  405. act_entry, p_info->temp_buf + len);
  406. if (view->format_proc)
  407. len += view->format_proc(id_snap, view, p_info->temp_buf + len,
  408. DEBUG_DATA(act_entry));
  409. out:
  410. return len;
  411. }
  412. /*
  413. * debug_next_entry:
  414. * - goto next entry in p_info
  415. */
  416. static inline int
  417. debug_next_entry(file_private_info_t *p_info)
  418. {
  419. debug_info_t *id;
  420. id = p_info->debug_info_snap;
  421. if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
  422. p_info->act_entry = 0;
  423. p_info->act_page = 0;
  424. goto out;
  425. }
  426. if(!id->areas)
  427. return 1;
  428. p_info->act_entry += id->entry_size;
  429. /* switch to next page, if we reached the end of the page */
  430. if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
  431. /* next page */
  432. p_info->act_entry = 0;
  433. p_info->act_page += 1;
  434. if((p_info->act_page % id->pages_per_area) == 0) {
  435. /* next area */
  436. p_info->act_area++;
  437. p_info->act_page=0;
  438. }
  439. if(p_info->act_area >= id->nr_areas)
  440. return 1;
  441. }
  442. out:
  443. return 0;
  444. }
  445. /*
  446. * debug_output:
  447. * - called for user read()
  448. * - copies formated debug entries to the user buffer
  449. */
  450. static ssize_t
  451. debug_output(struct file *file, /* file descriptor */
  452. char __user *user_buf, /* user buffer */
  453. size_t len, /* length of buffer */
  454. loff_t *offset) /* offset in the file */
  455. {
  456. size_t count = 0;
  457. size_t entry_offset;
  458. file_private_info_t *p_info;
  459. p_info = ((file_private_info_t *) file->private_data);
  460. if (*offset != p_info->offset)
  461. return -EPIPE;
  462. if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
  463. return 0;
  464. entry_offset = p_info->act_entry_offset;
  465. while(count < len){
  466. int formatted_line_size;
  467. int formatted_line_residue;
  468. int user_buf_residue;
  469. size_t copy_size;
  470. formatted_line_size = debug_format_entry(p_info);
  471. formatted_line_residue = formatted_line_size - entry_offset;
  472. user_buf_residue = len-count;
  473. copy_size = min(user_buf_residue, formatted_line_residue);
  474. if(copy_size){
  475. if (copy_to_user(user_buf + count, p_info->temp_buf
  476. + entry_offset, copy_size))
  477. return -EFAULT;
  478. count += copy_size;
  479. entry_offset += copy_size;
  480. }
  481. if(copy_size == formatted_line_residue){
  482. entry_offset = 0;
  483. if(debug_next_entry(p_info))
  484. goto out;
  485. }
  486. }
  487. out:
  488. p_info->offset = *offset + count;
  489. p_info->act_entry_offset = entry_offset;
  490. *offset = p_info->offset;
  491. return count;
  492. }
  493. /*
  494. * debug_input:
  495. * - called for user write()
  496. * - calls input function of view
  497. */
  498. static ssize_t
  499. debug_input(struct file *file, const char __user *user_buf, size_t length,
  500. loff_t *offset)
  501. {
  502. int rc = 0;
  503. file_private_info_t *p_info;
  504. down(&debug_lock);
  505. p_info = ((file_private_info_t *) file->private_data);
  506. if (p_info->view->input_proc)
  507. rc = p_info->view->input_proc(p_info->debug_info_org,
  508. p_info->view, file, user_buf,
  509. length, offset);
  510. else
  511. rc = -EPERM;
  512. up(&debug_lock);
  513. return rc; /* number of input characters */
  514. }
  515. /*
  516. * debug_open:
  517. * - called for user open()
  518. * - copies formated output to private_data area of the file
  519. * handle
  520. */
  521. static int
  522. debug_open(struct inode *inode, struct file *file)
  523. {
  524. int i = 0, rc = 0;
  525. file_private_info_t *p_info;
  526. debug_info_t *debug_info, *debug_info_snapshot;
  527. down(&debug_lock);
  528. debug_info = file->f_dentry->d_inode->i_private;
  529. /* find debug view */
  530. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  531. if (!debug_info->views[i])
  532. continue;
  533. else if (debug_info->debugfs_entries[i] ==
  534. file->f_dentry) {
  535. goto found; /* found view ! */
  536. }
  537. }
  538. /* no entry found */
  539. rc = -EINVAL;
  540. goto out;
  541. found:
  542. /* Make snapshot of current debug areas to get it consistent. */
  543. /* To copy all the areas is only needed, if we have a view which */
  544. /* formats the debug areas. */
  545. if(!debug_info->views[i]->format_proc &&
  546. !debug_info->views[i]->header_proc){
  547. debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
  548. } else {
  549. debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
  550. }
  551. if(!debug_info_snapshot){
  552. rc = -ENOMEM;
  553. goto out;
  554. }
  555. p_info = (file_private_info_t *) kmalloc(sizeof(file_private_info_t),
  556. GFP_KERNEL);
  557. if(!p_info){
  558. if(debug_info_snapshot)
  559. debug_info_free(debug_info_snapshot);
  560. rc = -ENOMEM;
  561. goto out;
  562. }
  563. p_info->offset = 0;
  564. p_info->debug_info_snap = debug_info_snapshot;
  565. p_info->debug_info_org = debug_info;
  566. p_info->view = debug_info->views[i];
  567. p_info->act_area = 0;
  568. p_info->act_page = 0;
  569. p_info->act_entry = DEBUG_PROLOG_ENTRY;
  570. p_info->act_entry_offset = 0;
  571. file->private_data = p_info;
  572. debug_info_get(debug_info);
  573. out:
  574. up(&debug_lock);
  575. return rc;
  576. }
  577. /*
  578. * debug_close:
  579. * - called for user close()
  580. * - deletes private_data area of the file handle
  581. */
  582. static int
  583. debug_close(struct inode *inode, struct file *file)
  584. {
  585. file_private_info_t *p_info;
  586. p_info = (file_private_info_t *) file->private_data;
  587. if(p_info->debug_info_snap)
  588. debug_info_free(p_info->debug_info_snap);
  589. debug_info_put(p_info->debug_info_org);
  590. kfree(file->private_data);
  591. return 0; /* success */
  592. }
  593. /*
  594. * debug_register:
  595. * - creates and initializes debug area for the caller
  596. * - returns handle for debug area
  597. */
  598. debug_info_t*
  599. debug_register (char *name, int pages_per_area, int nr_areas, int buf_size)
  600. {
  601. debug_info_t *rc = NULL;
  602. if (!initialized)
  603. BUG();
  604. down(&debug_lock);
  605. /* create new debug_info */
  606. rc = debug_info_create(name, pages_per_area, nr_areas, buf_size);
  607. if(!rc)
  608. goto out;
  609. debug_register_view(rc, &debug_level_view);
  610. debug_register_view(rc, &debug_flush_view);
  611. debug_register_view(rc, &debug_pages_view);
  612. out:
  613. if (!rc){
  614. printk(KERN_ERR "debug: debug_register failed for %s\n",name);
  615. }
  616. up(&debug_lock);
  617. return rc;
  618. }
  619. /*
  620. * debug_unregister:
  621. * - give back debug area
  622. */
  623. void
  624. debug_unregister(debug_info_t * id)
  625. {
  626. if (!id)
  627. goto out;
  628. down(&debug_lock);
  629. debug_info_put(id);
  630. up(&debug_lock);
  631. out:
  632. return;
  633. }
  634. /*
  635. * debug_set_size:
  636. * - set area size (number of pages) and number of areas
  637. */
  638. static int
  639. debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
  640. {
  641. unsigned long flags;
  642. debug_entry_t *** new_areas;
  643. int rc=0;
  644. if(!id || (nr_areas <= 0) || (pages_per_area < 0))
  645. return -EINVAL;
  646. if(pages_per_area > 0){
  647. new_areas = debug_areas_alloc(pages_per_area, nr_areas);
  648. if(!new_areas) {
  649. printk(KERN_WARNING "debug: could not allocate memory "\
  650. "for pagenumber: %i\n",pages_per_area);
  651. rc = -ENOMEM;
  652. goto out;
  653. }
  654. } else {
  655. new_areas = NULL;
  656. }
  657. spin_lock_irqsave(&id->lock,flags);
  658. debug_areas_free(id);
  659. id->areas = new_areas;
  660. id->nr_areas = nr_areas;
  661. id->pages_per_area = pages_per_area;
  662. id->active_area = 0;
  663. memset(id->active_entries,0,sizeof(int)*id->nr_areas);
  664. memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
  665. spin_unlock_irqrestore(&id->lock,flags);
  666. printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
  667. ,id->name, pages_per_area);
  668. out:
  669. return rc;
  670. }
  671. /*
  672. * debug_set_level:
  673. * - set actual debug level
  674. */
  675. void
  676. debug_set_level(debug_info_t* id, int new_level)
  677. {
  678. unsigned long flags;
  679. if(!id)
  680. return;
  681. spin_lock_irqsave(&id->lock,flags);
  682. if(new_level == DEBUG_OFF_LEVEL){
  683. id->level = DEBUG_OFF_LEVEL;
  684. printk(KERN_INFO "debug: %s: switched off\n",id->name);
  685. } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
  686. printk(KERN_INFO
  687. "debug: %s: level %i is out of range (%i - %i)\n",
  688. id->name, new_level, 0, DEBUG_MAX_LEVEL);
  689. } else {
  690. id->level = new_level;
  691. }
  692. spin_unlock_irqrestore(&id->lock,flags);
  693. }
  694. /*
  695. * proceed_active_entry:
  696. * - set active entry to next in the ring buffer
  697. */
  698. static inline void
  699. proceed_active_entry(debug_info_t * id)
  700. {
  701. if ((id->active_entries[id->active_area] += id->entry_size)
  702. > (PAGE_SIZE - id->entry_size)){
  703. id->active_entries[id->active_area] = 0;
  704. id->active_pages[id->active_area] =
  705. (id->active_pages[id->active_area] + 1) %
  706. id->pages_per_area;
  707. }
  708. }
  709. /*
  710. * proceed_active_area:
  711. * - set active area to next in the ring buffer
  712. */
  713. static inline void
  714. proceed_active_area(debug_info_t * id)
  715. {
  716. id->active_area++;
  717. id->active_area = id->active_area % id->nr_areas;
  718. }
  719. /*
  720. * get_active_entry:
  721. */
  722. static inline debug_entry_t*
  723. get_active_entry(debug_info_t * id)
  724. {
  725. return (debug_entry_t *) (((char *) id->areas[id->active_area]
  726. [id->active_pages[id->active_area]]) +
  727. id->active_entries[id->active_area]);
  728. }
  729. /*
  730. * debug_finish_entry:
  731. * - set timestamp, caller address, cpu number etc.
  732. */
  733. static inline void
  734. debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
  735. int exception)
  736. {
  737. active->id.stck = get_clock();
  738. active->id.fields.cpuid = smp_processor_id();
  739. active->caller = __builtin_return_address(0);
  740. active->id.fields.exception = exception;
  741. active->id.fields.level = level;
  742. proceed_active_entry(id);
  743. if(exception)
  744. proceed_active_area(id);
  745. }
  746. static int debug_stoppable=1;
  747. static int debug_active=1;
  748. #define CTL_S390DBF 5677
  749. #define CTL_S390DBF_STOPPABLE 5678
  750. #define CTL_S390DBF_ACTIVE 5679
  751. /*
  752. * proc handler for the running debug_active sysctl
  753. * always allow read, allow write only if debug_stoppable is set or
  754. * if debug_active is already off
  755. */
  756. static int
  757. s390dbf_procactive(ctl_table *table, int write, struct file *filp,
  758. void __user *buffer, size_t *lenp, loff_t *ppos)
  759. {
  760. if (!write || debug_stoppable || !debug_active)
  761. return proc_dointvec(table, write, filp, buffer, lenp, ppos);
  762. else
  763. return 0;
  764. }
  765. static struct ctl_table s390dbf_table[] = {
  766. {
  767. .ctl_name = CTL_S390DBF_STOPPABLE,
  768. .procname = "debug_stoppable",
  769. .data = &debug_stoppable,
  770. .maxlen = sizeof(int),
  771. .mode = S_IRUGO | S_IWUSR,
  772. .proc_handler = &proc_dointvec,
  773. .strategy = &sysctl_intvec,
  774. },
  775. {
  776. .ctl_name = CTL_S390DBF_ACTIVE,
  777. .procname = "debug_active",
  778. .data = &debug_active,
  779. .maxlen = sizeof(int),
  780. .mode = S_IRUGO | S_IWUSR,
  781. .proc_handler = &s390dbf_procactive,
  782. .strategy = &sysctl_intvec,
  783. },
  784. { .ctl_name = 0 }
  785. };
  786. static struct ctl_table s390dbf_dir_table[] = {
  787. {
  788. .ctl_name = CTL_S390DBF,
  789. .procname = "s390dbf",
  790. .maxlen = 0,
  791. .mode = S_IRUGO | S_IXUGO,
  792. .child = s390dbf_table,
  793. },
  794. { .ctl_name = 0 }
  795. };
  796. struct ctl_table_header *s390dbf_sysctl_header;
  797. void
  798. debug_stop_all(void)
  799. {
  800. if (debug_stoppable)
  801. debug_active = 0;
  802. }
  803. /*
  804. * debug_event_common:
  805. * - write debug entry with given size
  806. */
  807. debug_entry_t*
  808. debug_event_common(debug_info_t * id, int level, const void *buf, int len)
  809. {
  810. unsigned long flags;
  811. debug_entry_t *active;
  812. if (!debug_active || !id->areas)
  813. return NULL;
  814. spin_lock_irqsave(&id->lock, flags);
  815. active = get_active_entry(id);
  816. memset(DEBUG_DATA(active), 0, id->buf_size);
  817. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  818. debug_finish_entry(id, active, level, 0);
  819. spin_unlock_irqrestore(&id->lock, flags);
  820. return active;
  821. }
  822. /*
  823. * debug_exception_common:
  824. * - write debug entry with given size and switch to next debug area
  825. */
  826. debug_entry_t
  827. *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
  828. {
  829. unsigned long flags;
  830. debug_entry_t *active;
  831. if (!debug_active || !id->areas)
  832. return NULL;
  833. spin_lock_irqsave(&id->lock, flags);
  834. active = get_active_entry(id);
  835. memset(DEBUG_DATA(active), 0, id->buf_size);
  836. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  837. debug_finish_entry(id, active, level, 1);
  838. spin_unlock_irqrestore(&id->lock, flags);
  839. return active;
  840. }
  841. /*
  842. * counts arguments in format string for sprintf view
  843. */
  844. static inline int
  845. debug_count_numargs(char *string)
  846. {
  847. int numargs=0;
  848. while(*string) {
  849. if(*string++=='%')
  850. numargs++;
  851. }
  852. return(numargs);
  853. }
  854. /*
  855. * debug_sprintf_event:
  856. */
  857. debug_entry_t*
  858. debug_sprintf_event(debug_info_t* id, int level,char *string,...)
  859. {
  860. va_list ap;
  861. int numargs,idx;
  862. unsigned long flags;
  863. debug_sprintf_entry_t *curr_event;
  864. debug_entry_t *active;
  865. if((!id) || (level > id->level))
  866. return NULL;
  867. if (!debug_active || !id->areas)
  868. return NULL;
  869. numargs=debug_count_numargs(string);
  870. spin_lock_irqsave(&id->lock, flags);
  871. active = get_active_entry(id);
  872. curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
  873. va_start(ap,string);
  874. curr_event->string=string;
  875. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  876. curr_event->args[idx]=va_arg(ap,long);
  877. va_end(ap);
  878. debug_finish_entry(id, active, level, 0);
  879. spin_unlock_irqrestore(&id->lock, flags);
  880. return active;
  881. }
  882. /*
  883. * debug_sprintf_exception:
  884. */
  885. debug_entry_t*
  886. debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
  887. {
  888. va_list ap;
  889. int numargs,idx;
  890. unsigned long flags;
  891. debug_sprintf_entry_t *curr_event;
  892. debug_entry_t *active;
  893. if((!id) || (level > id->level))
  894. return NULL;
  895. if (!debug_active || !id->areas)
  896. return NULL;
  897. numargs=debug_count_numargs(string);
  898. spin_lock_irqsave(&id->lock, flags);
  899. active = get_active_entry(id);
  900. curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
  901. va_start(ap,string);
  902. curr_event->string=string;
  903. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  904. curr_event->args[idx]=va_arg(ap,long);
  905. va_end(ap);
  906. debug_finish_entry(id, active, level, 1);
  907. spin_unlock_irqrestore(&id->lock, flags);
  908. return active;
  909. }
  910. /*
  911. * debug_init:
  912. * - is called exactly once to initialize the debug feature
  913. */
  914. static int
  915. __init debug_init(void)
  916. {
  917. int rc = 0;
  918. s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table, 1);
  919. down(&debug_lock);
  920. debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
  921. printk(KERN_INFO "debug: Initialization complete\n");
  922. initialized = 1;
  923. up(&debug_lock);
  924. return rc;
  925. }
  926. /*
  927. * debug_register_view:
  928. */
  929. int
  930. debug_register_view(debug_info_t * id, struct debug_view *view)
  931. {
  932. int rc = 0;
  933. int i;
  934. unsigned long flags;
  935. mode_t mode = S_IFREG;
  936. struct dentry *pde;
  937. if (!id)
  938. goto out;
  939. if (view->prolog_proc || view->format_proc || view->header_proc)
  940. mode |= S_IRUSR;
  941. if (view->input_proc)
  942. mode |= S_IWUSR;
  943. pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
  944. id , &debug_file_ops);
  945. if (!pde){
  946. printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
  947. " Cannot register view %s/%s\n", id->name,view->name);
  948. rc = -1;
  949. goto out;
  950. }
  951. spin_lock_irqsave(&id->lock, flags);
  952. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  953. if (!id->views[i])
  954. break;
  955. }
  956. if (i == DEBUG_MAX_VIEWS) {
  957. printk(KERN_WARNING "debug: cannot register view %s/%s\n",
  958. id->name,view->name);
  959. printk(KERN_WARNING
  960. "debug: maximum number of views reached (%i)!\n", i);
  961. debugfs_remove(pde);
  962. rc = -1;
  963. } else {
  964. id->views[i] = view;
  965. id->debugfs_entries[i] = pde;
  966. }
  967. spin_unlock_irqrestore(&id->lock, flags);
  968. out:
  969. return rc;
  970. }
  971. /*
  972. * debug_unregister_view:
  973. */
  974. int
  975. debug_unregister_view(debug_info_t * id, struct debug_view *view)
  976. {
  977. int rc = 0;
  978. int i;
  979. unsigned long flags;
  980. if (!id)
  981. goto out;
  982. spin_lock_irqsave(&id->lock, flags);
  983. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  984. if (id->views[i] == view)
  985. break;
  986. }
  987. if (i == DEBUG_MAX_VIEWS)
  988. rc = -1;
  989. else {
  990. debugfs_remove(id->debugfs_entries[i]);
  991. id->views[i] = NULL;
  992. rc = 0;
  993. }
  994. spin_unlock_irqrestore(&id->lock, flags);
  995. out:
  996. return rc;
  997. }
  998. static inline char *
  999. debug_get_user_string(const char __user *user_buf, size_t user_len)
  1000. {
  1001. char* buffer;
  1002. buffer = kmalloc(user_len + 1, GFP_KERNEL);
  1003. if (!buffer)
  1004. return ERR_PTR(-ENOMEM);
  1005. if (copy_from_user(buffer, user_buf, user_len) != 0) {
  1006. kfree(buffer);
  1007. return ERR_PTR(-EFAULT);
  1008. }
  1009. /* got the string, now strip linefeed. */
  1010. if (buffer[user_len - 1] == '\n')
  1011. buffer[user_len - 1] = 0;
  1012. else
  1013. buffer[user_len] = 0;
  1014. return buffer;
  1015. }
  1016. static inline int
  1017. debug_get_uint(char *buf)
  1018. {
  1019. int rc;
  1020. for(; isspace(*buf); buf++);
  1021. rc = simple_strtoul(buf, &buf, 10);
  1022. if(*buf){
  1023. printk("debug: no integer specified!\n");
  1024. rc = -EINVAL;
  1025. }
  1026. return rc;
  1027. }
  1028. /*
  1029. * functions for debug-views
  1030. ***********************************
  1031. */
  1032. /*
  1033. * prints out actual debug level
  1034. */
  1035. static int
  1036. debug_prolog_pages_fn(debug_info_t * id,
  1037. struct debug_view *view, char *out_buf)
  1038. {
  1039. return sprintf(out_buf, "%i\n", id->pages_per_area);
  1040. }
  1041. /*
  1042. * reads new size (number of pages per debug area)
  1043. */
  1044. static int
  1045. debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
  1046. struct file *file, const char __user *user_buf,
  1047. size_t user_len, loff_t * offset)
  1048. {
  1049. char *str;
  1050. int rc,new_pages;
  1051. if (user_len > 0x10000)
  1052. user_len = 0x10000;
  1053. if (*offset != 0){
  1054. rc = -EPIPE;
  1055. goto out;
  1056. }
  1057. str = debug_get_user_string(user_buf,user_len);
  1058. if(IS_ERR(str)){
  1059. rc = PTR_ERR(str);
  1060. goto out;
  1061. }
  1062. new_pages = debug_get_uint(str);
  1063. if(new_pages < 0){
  1064. rc = -EINVAL;
  1065. goto free_str;
  1066. }
  1067. rc = debug_set_size(id,id->nr_areas, new_pages);
  1068. if(rc != 0){
  1069. rc = -EINVAL;
  1070. goto free_str;
  1071. }
  1072. rc = user_len;
  1073. free_str:
  1074. kfree(str);
  1075. out:
  1076. *offset += user_len;
  1077. return rc; /* number of input characters */
  1078. }
  1079. /*
  1080. * prints out actual debug level
  1081. */
  1082. static int
  1083. debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
  1084. {
  1085. int rc = 0;
  1086. if(id->level == DEBUG_OFF_LEVEL) {
  1087. rc = sprintf(out_buf,"-\n");
  1088. }
  1089. else {
  1090. rc = sprintf(out_buf, "%i\n", id->level);
  1091. }
  1092. return rc;
  1093. }
  1094. /*
  1095. * reads new debug level
  1096. */
  1097. static int
  1098. debug_input_level_fn(debug_info_t * id, struct debug_view *view,
  1099. struct file *file, const char __user *user_buf,
  1100. size_t user_len, loff_t * offset)
  1101. {
  1102. char *str;
  1103. int rc,new_level;
  1104. if (user_len > 0x10000)
  1105. user_len = 0x10000;
  1106. if (*offset != 0){
  1107. rc = -EPIPE;
  1108. goto out;
  1109. }
  1110. str = debug_get_user_string(user_buf,user_len);
  1111. if(IS_ERR(str)){
  1112. rc = PTR_ERR(str);
  1113. goto out;
  1114. }
  1115. if(str[0] == '-'){
  1116. debug_set_level(id, DEBUG_OFF_LEVEL);
  1117. rc = user_len;
  1118. goto free_str;
  1119. } else {
  1120. new_level = debug_get_uint(str);
  1121. }
  1122. if(new_level < 0) {
  1123. printk(KERN_INFO "debug: level `%s` is not valid\n", str);
  1124. rc = -EINVAL;
  1125. } else {
  1126. debug_set_level(id, new_level);
  1127. rc = user_len;
  1128. }
  1129. free_str:
  1130. kfree(str);
  1131. out:
  1132. *offset += user_len;
  1133. return rc; /* number of input characters */
  1134. }
  1135. /*
  1136. * flushes debug areas
  1137. */
  1138. void
  1139. debug_flush(debug_info_t* id, int area)
  1140. {
  1141. unsigned long flags;
  1142. int i,j;
  1143. if(!id || !id->areas)
  1144. return;
  1145. spin_lock_irqsave(&id->lock,flags);
  1146. if(area == DEBUG_FLUSH_ALL){
  1147. id->active_area = 0;
  1148. memset(id->active_entries, 0, id->nr_areas * sizeof(int));
  1149. for (i = 0; i < id->nr_areas; i++) {
  1150. id->active_pages[i] = 0;
  1151. for(j = 0; j < id->pages_per_area; j++) {
  1152. memset(id->areas[i][j], 0, PAGE_SIZE);
  1153. }
  1154. }
  1155. printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
  1156. } else if(area >= 0 && area < id->nr_areas) {
  1157. id->active_entries[area] = 0;
  1158. id->active_pages[area] = 0;
  1159. for(i = 0; i < id->pages_per_area; i++) {
  1160. memset(id->areas[area][i],0,PAGE_SIZE);
  1161. }
  1162. printk(KERN_INFO "debug: %s: area %i has been flushed\n",
  1163. id->name, area);
  1164. } else {
  1165. printk(KERN_INFO
  1166. "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
  1167. id->name, area, 0, id->nr_areas-1);
  1168. }
  1169. spin_unlock_irqrestore(&id->lock,flags);
  1170. }
  1171. /*
  1172. * view function: flushes debug areas
  1173. */
  1174. static int
  1175. debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
  1176. struct file *file, const char __user *user_buf,
  1177. size_t user_len, loff_t * offset)
  1178. {
  1179. char input_buf[1];
  1180. int rc = user_len;
  1181. if (user_len > 0x10000)
  1182. user_len = 0x10000;
  1183. if (*offset != 0){
  1184. rc = -EPIPE;
  1185. goto out;
  1186. }
  1187. if (copy_from_user(input_buf, user_buf, 1)){
  1188. rc = -EFAULT;
  1189. goto out;
  1190. }
  1191. if(input_buf[0] == '-') {
  1192. debug_flush(id, DEBUG_FLUSH_ALL);
  1193. goto out;
  1194. }
  1195. if (isdigit(input_buf[0])) {
  1196. int area = ((int) input_buf[0] - (int) '0');
  1197. debug_flush(id, area);
  1198. goto out;
  1199. }
  1200. printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
  1201. out:
  1202. *offset += user_len;
  1203. return rc; /* number of input characters */
  1204. }
  1205. /*
  1206. * prints debug header in raw format
  1207. */
  1208. static int
  1209. debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
  1210. int area, debug_entry_t * entry, char *out_buf)
  1211. {
  1212. int rc;
  1213. rc = sizeof(debug_entry_t);
  1214. memcpy(out_buf,entry,sizeof(debug_entry_t));
  1215. return rc;
  1216. }
  1217. /*
  1218. * prints debug data in raw format
  1219. */
  1220. static int
  1221. debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
  1222. char *out_buf, const char *in_buf)
  1223. {
  1224. int rc;
  1225. rc = id->buf_size;
  1226. memcpy(out_buf, in_buf, id->buf_size);
  1227. return rc;
  1228. }
  1229. /*
  1230. * prints debug data in hex/ascii format
  1231. */
  1232. static int
  1233. debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
  1234. char *out_buf, const char *in_buf)
  1235. {
  1236. int i, rc = 0;
  1237. for (i = 0; i < id->buf_size; i++) {
  1238. rc += sprintf(out_buf + rc, "%02x ",
  1239. ((unsigned char *) in_buf)[i]);
  1240. }
  1241. rc += sprintf(out_buf + rc, "| ");
  1242. for (i = 0; i < id->buf_size; i++) {
  1243. unsigned char c = in_buf[i];
  1244. if (!isprint(c))
  1245. rc += sprintf(out_buf + rc, ".");
  1246. else
  1247. rc += sprintf(out_buf + rc, "%c", c);
  1248. }
  1249. rc += sprintf(out_buf + rc, "\n");
  1250. return rc;
  1251. }
  1252. /*
  1253. * prints header for debug entry
  1254. */
  1255. int
  1256. debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
  1257. int area, debug_entry_t * entry, char *out_buf)
  1258. {
  1259. struct timespec time_spec;
  1260. unsigned long long time;
  1261. char *except_str;
  1262. unsigned long caller;
  1263. int rc = 0;
  1264. unsigned int level;
  1265. level = entry->id.fields.level;
  1266. time = entry->id.stck;
  1267. /* adjust todclock to 1970 */
  1268. time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
  1269. tod_to_timeval(time, &time_spec);
  1270. if (entry->id.fields.exception)
  1271. except_str = "*";
  1272. else
  1273. except_str = "-";
  1274. caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
  1275. rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
  1276. area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
  1277. except_str, entry->id.fields.cpuid, (void *) caller);
  1278. return rc;
  1279. }
  1280. /*
  1281. * prints debug data sprintf-formated:
  1282. * debug_sprinf_event/exception calls must be used together with this view
  1283. */
  1284. #define DEBUG_SPRINTF_MAX_ARGS 10
  1285. static int
  1286. debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
  1287. char *out_buf, debug_sprintf_entry_t *curr_event)
  1288. {
  1289. int num_longs, num_used_args = 0,i, rc = 0;
  1290. int index[DEBUG_SPRINTF_MAX_ARGS];
  1291. /* count of longs fit into one entry */
  1292. num_longs = id->buf_size / sizeof(long);
  1293. if(num_longs < 1)
  1294. goto out; /* bufsize of entry too small */
  1295. if(num_longs == 1) {
  1296. /* no args, we use only the string */
  1297. strcpy(out_buf, curr_event->string);
  1298. rc = strlen(curr_event->string);
  1299. goto out;
  1300. }
  1301. /* number of arguments used for sprintf (without the format string) */
  1302. num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
  1303. memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
  1304. for(i = 0; i < num_used_args; i++)
  1305. index[i] = i;
  1306. rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
  1307. curr_event->args[index[1]], curr_event->args[index[2]],
  1308. curr_event->args[index[3]], curr_event->args[index[4]],
  1309. curr_event->args[index[5]], curr_event->args[index[6]],
  1310. curr_event->args[index[7]], curr_event->args[index[8]],
  1311. curr_event->args[index[9]]);
  1312. out:
  1313. return rc;
  1314. }
  1315. /*
  1316. * clean up module
  1317. */
  1318. void
  1319. __exit debug_exit(void)
  1320. {
  1321. debugfs_remove(debug_debugfs_root_entry);
  1322. unregister_sysctl_table(s390dbf_sysctl_header);
  1323. return;
  1324. }
  1325. /*
  1326. * module definitions
  1327. */
  1328. postcore_initcall(debug_init);
  1329. module_exit(debug_exit);
  1330. MODULE_LICENSE("GPL");
  1331. EXPORT_SYMBOL(debug_register);
  1332. EXPORT_SYMBOL(debug_unregister);
  1333. EXPORT_SYMBOL(debug_set_level);
  1334. EXPORT_SYMBOL(debug_stop_all);
  1335. EXPORT_SYMBOL(debug_register_view);
  1336. EXPORT_SYMBOL(debug_unregister_view);
  1337. EXPORT_SYMBOL(debug_event_common);
  1338. EXPORT_SYMBOL(debug_exception_common);
  1339. EXPORT_SYMBOL(debug_hex_ascii_view);
  1340. EXPORT_SYMBOL(debug_raw_view);
  1341. EXPORT_SYMBOL(debug_dflt_header_fn);
  1342. EXPORT_SYMBOL(debug_sprintf_view);
  1343. EXPORT_SYMBOL(debug_sprintf_exception);
  1344. EXPORT_SYMBOL(debug_sprintf_event);