debug.c 35 KB

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