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